Python Bulk Modify File name

Source: Internet
Author: User

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

Related Article

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.