# -*- coding: utf-8 -*- import os path = "d:\\curl\data\\" for file in os.listdir(path): print file if(os.path.isfile(os.path.join(path,file))==True): if file.find('.')>0: newname="new_"+file os.rename(os.path.join(path,file),os.path.join(path,newname)) print file,'ok'
代码2:
# -*- coding: utf-8 -*- import os def BatchRename(path,pattern): os.chdir(path) fileList=os.listdir(path) dotIndex = pattern.rfind('.') fileName = pattern[ : dotIndex] fileExt = pattern[dotIndex : ] genNum = 0 for fileItem in fileList: fileFullName = fileName + '_' + str(genNum) + fileExt os.rename(fileItem, fileFullName) print (fileItem + ' => ' + fileFullName) genNum += 1 if __name__ == '__main__': BatchRename("d:\\curl\\data","test.log")
代码3:
import os os.chdir("d:\\curl\\data") for file in os.listdir("d:\\curl\\data"): print file if(os.path.splitext(file)[1] == ".log"): print "yes" os.rename(file, os.path.splitext(file)[0]+".jpg")