Recently downloaded several American dramas (jailbreak, Lost, right game ...) ), each set has a good long file name. Want to shorten some, but one is too tired, so wrote a script to achieve batch modification:
Modified before file name:
Modified file name:
Code implementation:
#Encoding=utf-8ImportOSdefrename (path,newname):#get all file names and directory names under the path directoryFilenames =os.listdir (path) forFileNameinchFilenames:#decide if it's a file or a directory ifOs.path.isdir (Os.path.join (path,filename)):Continue #gets the file suffix name (for example: MKV)filetype= Filename.rsplit ('.', 1) [1] forIinchXrange (1,len (filenames) +1): #keyword lookup (for example, finding files with e01/e02/e03 in the file name) ifFilename.find ('E'+str (i). Zfill (2)) >-1: #Modify file nameOs.rename (Os.path.join (Path,filename), Os.path.join (path,newname+u'Section'+str (i) +u'Set'+'.'+filetype)) Break Else: Continue PrintU'completed. '#Folder pathPath = u'e:\\ thunder \ \'#file directorynewname = u'first season of prison Break'#file pathPath = path +newname#Bulk Modify file names under a specified directoryRename (path, newname)
Python Bulk Modify File name