Created
February 10, 2019 15:33
-
-
Save alexbirdaudio/512d4dade1fae2fb12985c34b0eccc75 to your computer and use it in GitHub Desktop.
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
import os | |
import sys | |
import csv | |
print('Hey') | |
def modify(name, n): | |
return name+'_'+str(n) | |
def rename(old, new, suffix=None): | |
from os import rename as os_rename | |
from os.path import exists | |
if suffix: | |
old, new = [name+'.'+suffix for name in (old, new)] | |
if exists(old) : os_rename(old, new) | |
memory = {} | |
for old, new in reader: | |
memory[new] = memory.setdefault(new, -1) + 1 | |
if memory[new]: | |
new = modify(new, memory[new]) | |
memory[new] = 0 | |
def rename_files(csv_filename): | |
with open('rename_csv.csv', 'r') as csvfile: | |
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"') | |
files_mapping = {} | |
new_filenames = set() | |
for row in csvreader: | |
#! add ' ' + before row[n] if files accidentally have space in front of int. | |
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): | |
print('\n' + 'renaming ' + name) | |
os.rename(name, new) | |
else: | |
print('\n' + name + " does not exist") | |
if __name__ == "__main__": | |
rename_files(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment