Python batch Modify file names,

Source: Internet
Author: User

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)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.