#-*-coding:utf-8-*-ImportOS, Sys,re,shutil fromNtImportchdir#read Chinese path u 'Path=u"d:\\zhyue93\\backup\\ personal files \ \ video \\10, c#\\4, C # language advanced features"dirs=os.listdir (path)#Sort 1,2,3...10,11dirs=sorted (Dirs,key =LambdaI:int (Re.match (R'(\d+)', i). Group ()))#Renaming a filei=10 forDirinchdirs:i+=1Newdir=Os.path.join (path,dir) newfiles=Os.listdir (newdir) forFileinchNewfiles:#before rename, use the ChDir () function to enter the path where the target file is located, #tell the Python compiler where to rename the file before you can modify #change the current working directory to the specified pathchdir (newdir) os.rename (File,str (i)+"-"+file)Print '-----' #move a file to a path pathPrintU'Moving Files' forDirinchDirs:newdir=Os.path.join (path,dir) newfiles=Os.listdir (newdir) forFileinchNewfiles:oldfilepath=Os.path.join (newdir,file) Newfilepath=Os.path.join (path,file) shutil.move (Oldfilepath, Newfilepath)#move file to destination path
Python bulk modifies file names and moves files to the specified directory