#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