Created
June 20, 2017 21:58
-
-
Save shreyb/a373fb3b72506c57b63e3d7b0bdb46b5 to your computer and use it in GitHub Desktop.
Quick script to go through JPEGS and copy files created after a certain date to a new subdirectory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import subprocess | |
import sys | |
import os | |
from datetime import datetime | |
from time import sleep | |
from shutil import copy2, rmtree | |
import re | |
import json | |
# Note: for this to run, exiftool must be installed. If not, go to http://owl.phy.queensu.ca/~phil/exiftool/ to grab it | |
JPGPAT = re.compile('\.jpg$', re.IGNORECASE) | |
newdir = 'NewPics' | |
countinrow, countskip = 0, 0 | |
def getdatefromuser(): | |
"""Get the cutoff date from user (stdin), return a date object for use in the main execution block""" | |
date_str = raw_input("\nEnter the date cutoff in mm/dd/yyyy format: ") | |
date_sub = date_str.replace('-', '/') | |
return datetime.strptime(date_sub, '%m/%d/%Y') | |
def rmdir(d): | |
"""Remove old directory if it exists, then create or recreate it. Sleep so user can see progress""" | |
print "Trying to remove old", d, "directory" | |
try: | |
rmtree(d) | |
except OSError: | |
print "No directory", d, ". Skipping removal." | |
pass | |
finally: | |
sleep(2) | |
os.mkdir(d) | |
print 'Created new directory', d | |
sleep(2) | |
def findcreatedate(f): | |
"""Finds when the JPEG file f was created""" | |
try: | |
exifdata = subprocess.check_output(['exiftool', '-j', '-TAG', '-CreateDate', f]) | |
except OSError as e: | |
print "exiftool may not be installed. Please go check." | |
print "Here is the error thrown: {}".format(e) | |
raise | |
data = json.loads(exifdata) | |
if isinstance(data, list) and len(data) == 1: | |
edata = data[0] | |
return datetime.strptime(edata[u'CreateDate'], '%Y:%m:%d %H:%M:%S') | |
else: | |
print "exiftool returned malformed data. Please investigate" | |
print data | |
sys.exit(1) | |
def fileparse(f): | |
"""Parses the files list. Uses findcreatedate to determine whether or not to copy file | |
""" | |
global countinrow, countskip, userdate, newdir | |
if JPGPAT.search(f): # We have a jpg or JPG file | |
if findcreatedate(f) > userdate: # Test file's creation date against date passed into function | |
if countskip != 0: # If this is the first time (locally) we've found a file that needs to be copied, reset j | |
print "\n", | |
countskip = 0 | |
dest = os.path.join(newdir, f) | |
copy2(f, dest) | |
print "\r{} being copied".format(f), | |
countinrow += 1 # increment our count of files that have been copied in a row | |
else: | |
countskip += 1 # otherwise, increment our count of files skipped | |
print "\r{} files parsed after last success".format(countskip), | |
return | |
def main(): | |
global countinrow, userdate | |
try: | |
print "Locating exiftool:\n" | |
subprocess.check_call(['which', 'exiftool']) | |
except subprocess.CalledProcessError as e: | |
print "Error: for this to run, exiftool must be installed. If not, go to http://owl.phy.queensu.ca/~phil/exiftool/ to grab it" | |
print e | |
sys.exit(1) | |
userdate = getdatefromuser() # get cutoff date from user, split it to pass in later to fileparse | |
rmdir(newdir) # set up our copy directory | |
files = (f for f in os.listdir(os.getcwd())) | |
for f in files: | |
fileparse(f) # work through the list of files, copying or skipping as needed | |
print "\n{} files copied".format(countinrow) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment