guxiao05 发表于 2013-1-15 02:49:43

Python批量修改文件后缀脚本

import os,stringdef s_rename(path,old_ext,new_ext):   for (path, dirs, files) in os.walk(path):         for filename in files:             ext=os.path.splitext(filename)             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"
页: [1]
查看完整版本: Python批量修改文件后缀脚本