Python bulk rename file suffix

Source: Internet
Author: User

#batch_file_rename.py#created:6th August" "This would batch rename a group of files in a given directory,once you pass the current and new extensions" "__author__='Craig Richards'__version__='1.0'ImportOSImportSYSdefbatch_rename (Work_dir, Old_ext, new_ext):" "This would batch rename a group of files in a given directory, once you pass the current and new extensions /c8>" "    #files = Os.listdir (Work_dir)     forFileNameinchOs.listdir (work_dir):#Get the file extensionFile_ext = os.path.splitext (filename) [1]        #Start of the logic to check the file extensions, if Old_ext = File_ext        ifOld_ext = =File_ext:#Set NewFile to is the filename, replaced with the new extensionNewFile =filename.replace (Old_ext, New_ext)#Write the filesOs.rename (Os.path.join (work_dir, filename), Os.path.join (Work_dir, NewFile) )defMain ():" "This would be called if the script is directly invoked. " "    #Set the variable Work_dir with the first argument passedWork_dir = sys.argv[1]    #Set the variable old_ext with the second argument passedOld_ext = sys.argv[2]    #Set the variable new_ext with the third argument passedNew_ext = sys.argv[3] Batch_rename (Work_dir, Old_ext, New_ext)if __name__=='__main__': Main ()

Python bulk rename file suffix

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.