Skip to content

Instantly share code, notes, and snippets.

@mozz100
Created May 11, 2014 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mozz100/7b7a803939aa094cb15c to your computer and use it in GitHub Desktop.
Save mozz100/7b7a803939aa094cb15c to your computer and use it in GitHub Desktop.
Rename files in order, move to a directory
#!/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