Skip to content

Instantly share code, notes, and snippets.

@rabbleNaut
Last active August 29, 2015 14:24
Show Gist options
  • Save rabbleNaut/679293a5e377d147701c to your computer and use it in GitHub Desktop.
Save rabbleNaut/679293a5e377d147701c to your computer and use it in GitHub Desktop.
'''
Used for renaming photos in a folder using a matching ID
7/7/2015
'''
import os, csv
#declare new dictionary
IDs = {}
#build dictionary using input csv file. csv should be setup as key:value pairs
with open('ids_master.csv','r') as inputcsv:
idReader = csv.reader(inputcsv, delimiter = ',')
for row in idReader:
IDs[row[0]] = row[1]
#path to folder with photos
photoPath = r'O:\Projects\City of Mandeville Louisiana\Fieldwork\TIFF Photos Newly Surveyed\Resource ID TIFFs'
#counter
count = 0
for photo in os.listdir(photoPath):
head, sep, tail = photo.partition('_')
#cmecID = photo[:5] #just first 5
cmecID = head
try:
if cmecID in IDs:
shpID = IDs[cmecID]
newName = shpID + sep + tail
print 'Renaming ' + newName + '...'
print photoPath + os.sep + photo
os.rename(photoPath + os.sep + photo, photoPath + os.sep + newName)
count += 1
else: print """
NO MATCH FOR FILE {0}
""".format(photo)
except Exception as e:
print """
{0}
""".format(e)
print """
{0} photo's were renamed.
""".format(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment