AnsweredAssumed Answered

rename files to match their EXIF “Date Taken”?

Question asked by jpilbeam Champion on Apr 16, 2019
Latest reply on Apr 16, 2019 by jpilbeam

I need some help putting the date in the filenames. Here's the working code I have so far. 

 

import arcpy, sys
import exifread
from exifread import exif
import os
import time
from datetime import datetime

im = r"path\to\DOT_SignInventory"

for root, dirnames, filenames in os.walk(im): #iterate directory
    for fname in filenames:
        if fname.endswith('.JPG'):
            with open(os.path.join(root, fname), 'rb') as image: #file path and name
                exif = exifread.process_file(image)
                dt = str(exif['EXIF DateTimeOriginal']) #get 'Date Taken' from JPG
                ds = time.strptime(dt, '%Y:%m:%d %H:%M:%S')
                nt = time.strftime("%m/%d/%Y",ds)#variable with 'Date Taken'
                print("Photo:{} Date Taken:{}".format(fname, "_" + nt))

 

I've tried these couple things after the print statement, but no cigar:

new_file = fname + nt + ".jpg"
f = open(fname.format(nt), "w")
f.write(fname + nt)
f.close

Outcomes