Photo & Video Sorting Script

I made a small python script that sorts photos and videos from my camera or phone. It creates directories with a timestamp in their names. The timestamp is based on EXIF data.

After running the script I add a short description of the event to the directory name. I remove the ‘TBD’ after cleaning up the photos in Lightroom

import os
import shutil
import datetime
import exifread


for root, dirs, filenames in os.walk('.'):
   for fname in filenames:
      strSrc = os.path.join(root, fname)
      ext = fname.lower().split('.')[-1]
      strDest = ''
      if ext == 'jpg' or ext == 'jpeg':
          try:
             fid = open(strSrc, 'rb')
             tags = exifread.process_file(fid)
             fid.close()
             d = str(tags['EXIF DateTimeOriginal'])
             strDir  = ext + '\\TBD_' + d[0:4] + d[5:7] + d[8:10]
             strDest = strDir + "\\" + fname
          except:
             strDir  = ext + '_noexif'
             strDest = strDir + "\\" + fname
      elif ext == 'mov' or ext == 'mp4':
          t = os.path.getmtime(strSrc) 
          v = datetime.datetime.fromtimestamp(t)
          strDir = ext + '\\TBD_' + v.strftime('%Y%m%d')
          strDest = strDir + "\\" + fname
      elif ext == 'png' or ext == 'aae':
          strDir = ext
          strDest = ext + "\\" + fname

      try:
         os.mkdir(ext)
      except:
         pass
      try:
         os.mkdir(strDir)
      except:
         pass
      if len(strDest) > 0:
        try:
           os.rename(strSrc, strDest)
           print("mv %s %s" %(strSrc,strDest))
        except:
           print("*** mv %s %s" %(strSrc,strDest))