Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python Code for Automatically Renaming and Filing Screenshots on the Mac
import os
import re
root = '/Users/jcur002/Dropbox/Screenshots'
Files = os.listdir(root)
pat1 = re.compile('Screenshot (?P<year>[0-9]{4})\-(?P<month>[0-9]{2})\-(?P<day>[0-9]{2}) (?P<hh>[0-9]{2})\.(?P<mm>[0-9]{2})\.(?P<ss>[0-9]{2})(\.png)?')
pat2 = re.compile('Screen Shot (?P<year>[0-9]{4})\-(?P<month>[0-9]{2})\-(?P<day>[0-9]{2}) at (?P<hh>[0-9]{1,2})\.(?P<mm>[0-9]{2})\.(?P<ss>[0-9]{2}) *(?P<am>AM|PM)*(\.png)?')
for f in Files:
m1 = pat1.match(f)
m2 = pat2.match(f)
if m1 or m2:
if m1:
m = m1
m = m2
year ='year')
month ='month')
day ='day')
hh ='hh')
mm ='mm')
ss ='ss')
if m2:
am = True if'am') == 'AM' else False
if not am:
hh = int(hh) + 12
hh = str(hh)
newname = '{year}-{month}-{day} {hh}.{mm}.{ss}.png'.format(year = year,
month = month,
day = day,
hh = hh,
mm = mm,
ss = ss)
dirPath = os.path.join(root, year)
if not os.path.isdir(dirPath):
oldPath = os.path.join(root, f)
newPath = os.path.join(root, year, newname)
os.rename(oldPath, newPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment