Last active
February 9, 2019 12:57
-
-
Save will-ockmore/587c8b5bf04e87bab2cda9d14f3dbac7 to your computer and use it in GitHub Desktop.
Run the whole thing as a script, will handle duplicate filenames.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import csv | |
def rename_files(csv_filename): | |
with open('CS_IVR_NOTES.csv', 'r') as csvfile: | |
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"') | |
files_mapping = {} | |
new_filenames = set() | |
for row in csvreader: | |
name = row[0] + '.wav' | |
new = row[1] + '.wav' | |
if new in new_filenames: | |
raise Exception( | |
'Found duplicate filename {} for file {}'.format(new, name) | |
) | |
new_filenames.add(new) | |
files_mapping['name'] = new | |
for name, new in files_mapping.items(): | |
if os.path.exists(name): | |
os.rename(name, new) | |
else: | |
print(name + " does not exist") | |
if __name__ == "__main__": | |
rename_files(sys.argv[1]) |
hmmm i'm getting the following return:
=========== RESTART: /Users/alexbird/Python/workflow/rename_ivr.py =========== Traceback (most recent call last): File "/Users/alexbird/Python/workflow/rename_ivr.py", line 36, in <module> rename_files(sys.argv[1]) IndexError: list index out of range
I'm not familiar with using sys.argv[n]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On the command line, run
assuming you're in the same directory as the script to make it executable.
Then to run it you can do: