Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. '''
  2. Used for renaming photos in a folder using a matching ID
  3. 7/7/2015
  4. '''
  5. import os, csv
  6.  
  7. #declare new dictionary
  8. IDs = {}
  9.  
  10. #build dictionary using input csv file. csv should be setup as key:value pairs
  11. with open('ids_master.csv','r') as inputcsv:
  12. idReader = csv.reader(inputcsv, delimiter = ',')
  13. for row in idReader:
  14. IDs[row[0]] = row[1]
  15.  
  16. #path to folder with photos
  17. photoPath = r'O:\Projects\City of Mandeville Louisiana\Fieldwork\TIFF Photos Newly Surveyed\Resource ID TIFFs'
  18.  
  19. #counter
  20. count = 0
  21.  
  22.  
  23. for photo in os.listdir(photoPath):
  24. head, sep, tail = photo.partition('_')
  25. #cmecID = photo[:5] #just first 5
  26. cmecID = head
  27. try:
  28. if cmecID in IDs:
  29. shpID = IDs[cmecID]
  30. newName = shpID + sep + tail
  31. print 'Renaming ' + newName + '...'
  32. print photoPath + os.sep + photo
  33.  
  34. os.rename(photoPath + os.sep + photo, photoPath + os.sep + newName)
  35. count += 1
  36. else: print """
  37. NO MATCH FOR FILE {0}
  38. """.format(photo)
  39. except Exception as e:
  40. print """
  41. {0}
  42. """.format(e)
  43.  
  44.  
  45. print """
  46. {0} photo's were renamed.
  47. """.format(count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement