Skip to content

Instantly share code, notes, and snippets.

@will-ockmore
Last active February 9, 2019 12:57
Show Gist options
  • Save will-ockmore/587c8b5bf04e87bab2cda9d14f3dbac7 to your computer and use it in GitHub Desktop.
Save will-ockmore/587c8b5bf04e87bab2cda9d14f3dbac7 to your computer and use it in GitHub Desktop.
Run the whole thing as a script, will handle duplicate filenames.
#!/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])
@alexbirdaudio
Copy link

alexbirdaudio commented Feb 9, 2019

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