From 4ad15c120959397acbaf4f15ca0930df560e22a9 Mon Sep 17 00:00:00 2001 From: DiabloCC Date: Sun, 13 Mar 2016 23:19:29 +0800 Subject: [PATCH] Update batch_file_rename.py --- batch_file_rename.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/batch_file_rename.py b/batch_file_rename.py index 0241141acf8..6f154c2435f 100644 --- a/batch_file_rename.py +++ b/batch_file_rename.py @@ -1,13 +1,16 @@ # batch_file_rename.py -# Created: 6th August 2012 +# copied: 2016-03-13 +# python version: 3.x ''' -This will batch rename a group of files in a given directory, +This will batch rename a group of files in a given directory, once you pass the current and new extensions ''' __author__ = 'Craig Richards' -__version__ = '1.0' +__changeby__ = 'Charlie Chen' +__originver__ = '1.0' +__version__ = '1.1' import os import sys @@ -23,7 +26,8 @@ def batch_rename(work_dir, old_ext, new_ext): # Get the file extension file_ext = os.path.splitext(filename)[1] # Start of the logic to check the file extensions, if old_ext = file_ext - if old_ext == file_ext: + # Charlie: splitext() returns the extension with a leading dot. + if file_ext == '.' + old_ext: # Set newfile to be the filename, replaced with the new extension newfile = filename.replace(old_ext, new_ext) # Write the files @@ -43,8 +47,21 @@ def main(): old_ext = sys.argv[2] # Set the variable new_ext with the third argument passed new_ext = sys.argv[3] + if (old_ext[0] == '.'): + r = input('The old extension has a leading dot. \nBy default, \ +file extensions may not include the leading dot. \ +\nDo you want remove the leading dot? (y|n) ') + if r == 'y': + old_ext = sys.argv[2][1:] + if (new_ext[0] == '.'): + r = input('The new extension has a leading dot. \nBy default \ +file extension does not include the leading dot. \ +\nDo you want remove the leading dot? (y|n) ') + if r == 'y': + new_ext = sys.argv[3][1:] batch_rename(work_dir, old_ext, new_ext) - + # Print the file list to see if the names are changed. + print('The file list in the given directory is:\n', os.listdir(work_dir)) if __name__ == '__main__': main()