|
6 | 6 |
|
7 | 7 | parser = argparse.ArgumentParser() |
8 | 8 | parser.add_argument('-d', '--directory', help='the directory of the files to be renamed. optional - if not provided, the script will ask for input') |
9 | | -parser.add_argument('-f', '--fileName', help='the CSV file of name changes. optional - if not provided, the script will ask for input') |
| 9 | +parser.add_argument('-f', '--fileNameCSV', help='the CSV file of name changes. optional - if not provided, the script will ask for input') |
10 | 10 | parser.add_argument('-m', '--makeChanges', help='Enter "true" to if the script should actually rename the files (otherwise, it will only create a log of the expected file name changes). optional - if not provided, the script will to "false"') |
11 | 11 | args = parser.parse_args() |
12 | 12 |
|
13 | 13 | if args.directory: |
14 | 14 | directory = args.directory |
15 | 15 | else: |
16 | 16 | directory = raw_input('Enter the directory of the files to be renamed: ') |
17 | | -if args.fileName: |
18 | | - fileName = args.fileName |
| 17 | +if args.fileNameCSV: |
| 18 | + fileNameCSV = args.fileNameCSV |
19 | 19 | else: |
20 | | - fileName = raw_input('Enter the CSV file of name changes (including \'.csv\'): ') |
| 20 | + fileNameCSV = raw_input('Enter the CSV file of name changes (including \'.csv\'): ') |
21 | 21 | if args.makeChanges: |
22 | 22 | makeChanges = args.makeChanges |
23 | 23 | else: |
|
28 | 28 | f.writerow(['oldFilename']+['newFilename']) |
29 | 29 | for root, dirs, files in os.walk(directory, topdown=True): |
30 | 30 | for file in files: |
31 | | - with open(nameChangeFile) as csvfile: |
| 31 | + with open(fileNameCSV) as csvfile: |
32 | 32 | reader = csv.DictReader(csvfile) |
33 | 33 | for row in reader: |
34 | 34 | oldFilename = row['file'] |
|
38 | 38 | oldPath = os.path.join(root,file) |
39 | 39 | newPath = os.path.join(root,newFilename) |
40 | 40 | f.writerow([oldPath]+[newPath]) |
41 | | - if makeChanges == 'true' |
| 41 | + if makeChanges == 'true': |
42 | 42 | os.rename(oldPath,newPath) |
43 | 43 | else: |
44 | 44 | print 'log of expected file name changes created only, no files renamed' |
|
0 commit comments