sorting photos with Python

As part of my MacBook replacement project, I wanted to get a good new backup of my photo library. Photos.app stores photos in a database, not individual files, so if you want a “plain old JPEG” backup of your library, you need to export the photos from Photos.app. There are various ways to do this, but the way I chose left me with a folder full of subfolders organized by “moment name,” so a typical subfolder would be named “Somerville, NJ, May 27, 2018” for instance. That’s kind of a mess, since the folder names sort alphabetically. (And the folder dates are all set to today. Setting the folder dates to the date the photos were taken would solve some problems, but let’s ignore that for now.)

To impose some rudimentary organization on this, I wanted to make a top-level folder for each year, and move all the subfolders for that year into that folder. Not perfect, but at least it gets me from having one folder with 2000 subfolders to 10 folders with 200 subfolders each (approximately), which is a bit more manageable.

I looked around at various ways to do this, and settled on writing a quick Python script, which is shown below. I haven’t written a Python program in years, so this probably isn’t elegant Python. But it works. I’m posting it here in case it’s useful for anyone else. (I first learned Python back in 2003, and have used it on and off since, but never regularly.)

I had also looked for third-party utility programs that would allow me to export from Photos with more options than Photos itself allows. I found an old MacStories article from 2012 that talked about exporting from iPhoto to Dropbox using a tool called Phoshare. I had hoped that maybe Phoshare had been kept up to date, and worked with Photos, but nope. It was last updated in 2012.

I feel like there’s probably some interesting stuff I could do with Hazel too, and/or ExifTool, but I didn’t want to over-complicate things. And I also feel like there’s probably a third-party app out there that would make this much easier, but I didn’t find one that did exactly what I was looking for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# sort_photo_bu.py
# ajh 2018-05-27
# sort Photos app export into folders by year.
# Export from Photos with subfolder format "moment name". 
# We assume the moment name always ends with the year.
# (Note that this may throw a "destination path already exists" error at some point, but that's probably fine.)
 
import os, shutil, sys
 
bupath = "/Users/andrew/Pictures/backup20180527"
for (root, dirs, files) in os.walk(bupath):
    for dirname in dirs:
        if len(dirname) > 4:
            year = dirname[-4:]
            yearpath = bupath + "/" + year
            if not os.path.isdir(yearpath):
                print "Making folder for " + year + "..."
                os.mkdir(yearpath)
            origpath = bupath + "/" + dirname
            #print "moving %s to %s..." % (origpath, yearpath)
            shutil.move(origpath, yearpath)
            #sys.exit()
print "All done."

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.