批量修改文件后缀名:

使用方法:s_rename(路径,原后缀,新后缀)

#coding:gbk #code By Neeao #2009-09-15 import os,string   def s_rename(path,old_ext,new_ext):     for (path, dirs, files) in os.walk(path):         for filename in files:             ext=os.path.splitext(filename)[1]             if (cmp(ext,old_ext)==0):                 newname=filename.replace(old_ext,new_ext)                 oldpath=path+"\\"+filename                 newpath=path+"\\"+newname                 print "oldpath:"+oldpath+""                 print "newpth:"+newpath+""                 try:                     os.rename(oldpath, newpath)                 except ValueError:                     print "Error when rename the file " + oldpath                 except NameError:                     print "Error when rename the file " + oldpath                 except OSError:                     #print OSError                     print newpath + " The file is already exist!" if __name__ == '__main__':     s_rename("F:\\code",".ph",".pl")     #print "test"
本文转自: 黑客武林(www.hack50.com) 详细出处参考:http://www.hack50.com/stu/sort094/sort0108/18715.html

 

相关文章:

  • 2021-06-21
  • 2021-08-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-20
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2021-04-16
  • 2022-12-23
相关资源
相似解决方案