Skip to content

Instantly share code, notes, and snippets.

@alexwright
Created February 14, 2013 19:14
Show Gist options
  • Save alexwright/4955446 to your computer and use it in GitHub Desktop.
Save alexwright/4955446 to your computer and use it in GitHub Desktop.
Quick and dirty script to sort pictures into a directory structure based on EXIF DateTaken date/times.
import os
import EXIF
import datetime
work_dir = '../pics'
exif_date_format = '%Y:%m:%d %H:%M:%S'
for dirname, dirnames, filenames in os.walk(work_dir):
for filename in filenames:
old_file_path = os.path.join(work_dir, filename)
file = open(old_file_path, 'rb')
tags = EXIF.process_file(file, stop_tag='Image DateTime')
if 'Image DateTime' in tags:
dt = datetime.datetime.strptime(tags['Image DateTime'].values, exif_date_format)
dir = dt.strftime('path/%Y/%m/%Y-%m-%d/')
if not os.path.exists(dir):
os.makedirs(dir)
new_file_path = os.path.join(dir, filename)
os.rename(old_file_path, new_file_path)
file.close()
@alexwright
Copy link
Author

Badly named variables and posiable exceptions on the makedirs, and I'm sure other places. But it does at least sort out my DCIM dir.

Uses exif-py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment