Python batch Modify file names,
Recently I downloaded several American TV series (jailbreaking, lost, right game...). The file name of each episode is very long .. I want to shorten it, but I am too tired to change it one by one, so I wrote a script to implement batch modification:
File name before modification:
Modified File Name:
Code implementation:
# Encoding = utf-8import osdef rename (path, newname): # Get all file names and directory names under the path directory filenames = OS. listdir (path) for filename in filenames: # determine whether it is a file or a directory if OS. path. isdir (OS. path. join (path, filename): continue # obtain the file extension name (for example, mkv) filetype = filename. rsplit ('. ', 1) [1] for I in xrange (1, len (filenames) + 1): # keyword search (for example: find the file with E01/E02/E03 in the file name.) if filename. find ('E' + str (I ). zfill (2)>-1: # modify the file name OS. rename (OS. path. join (pa Th, filename), OS. path. join (path, newname + u'quarter '+ str (I) + U' set' + '. '+ filetype) break else: continue print U' is complete. '# Folder path = U' E: \ Xunlei \ '# file directory newname = u'hell' # File path = path + newname # modify the rename (path, newname) file name in a specified directory in batches)