Skip to content

Instantly share code, notes, and snippets.

@BrechtDeMan
Created December 28, 2015 11:19
Show Gist options
  • Save BrechtDeMan/59076e4275aa80b4b24e to your computer and use it in GitHub Desktop.
Save BrechtDeMan/59076e4275aa80b4b24e to your computer and use it in GitHub Desktop.
Rename multiple files at once with list of original file names and list of corresponding resulting file names (for easy renaming using a text editor like Sublime Text)
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os # for getting files from directory
import sys # for accessing command line arguments
# Command line arguments
assert len(sys.argv)<4, "batchrename takes at most 1 command line argument\n"+\
"Use: python batchrename.py [folder_name]"
if len(sys.argv) == 1:
folder_name = "." # Looks in 'saves/' folder from 'scripts/' folder
print "Use: python batchrename.py [folder_name]"
print "Using default path: . (current folder)"
elif len(sys.argv) == 2:
folder_name = sys.argv[1] # First command line argument is folder
# translation table (TODO make better)
in_list = ['file1.txt','file2.txt','file3.txt']
out_list = ['textfile_1.txt','textfile_2.txt','textfile_3.txt']
# make sure slash at end of folder name
os.path.join(folder_name, '')
# get every WAV file in folder
files_list = os.listdir(folder_name)
for f in files_list: # iterate over all files in files_list
if f.endswith(".txt"): # check if TXT file (optional)
if f in in_list:
print f + ' becomes ' + out_list[in_list.index(f)] # DEBUG
os.rename(folder_name+f, folder_name+out_list[in_list.index(f)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment