Created
May 11, 2014 20:41
-
-
Save mozz100/7b7a803939aa094cb15c to your computer and use it in GitHub Desktop.
Rename files in order, move to a directory
This file contains 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/python | |
import sys, csv, os, shutil | |
pattern = "Vol09-%04d.jpg" | |
original_filename = sys.argv[1] | |
output_path = sys.argv[2] | |
output_csv = os.path.join(output_path, "original_filenames.csv") | |
next_index = 0 | |
if os.path.isfile(output_csv): | |
with open(output_csv, 'r') as csvfile: | |
reader = csv.reader(csvfile) | |
for row in reader: | |
next_index = int(row[0]) | |
next_index += 1 | |
# move and rename file | |
new_filename = pattern % (next_index, ) | |
shutil.move(original_filename, os.path.join(output_path, new_filename)) | |
# record what we did | |
with open(output_csv, 'a') as csvfile: | |
writer = csv.writer(csvfile) | |
writer.writerow([next_index, new_filename, os.path.basename(original_filename), ]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment