Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # imagefolder.py
- # Date: 03-03-2010
- # Author: Dominic Portain
- # Dependencies: Windows 5.0, Python 2.6, EXIF.py
- # Function: Moves all JPGs and TIFFs into subfolders of the format YYMMDD
- import os
- import EXIF
- import time
- path=str(os.getcwd()) # current working directory
- dirList = os.listdir(path)
- for fname in dirList:
- fnamesplit=fname.split(".") # split filename to get extensions
- if len(fnamesplit)>1: # file actually has an extension?
- if fnamesplit[-1] in ('jpg', 'JPG', 'jpeg', 'JPEG', 'JPE', 'TIF', 'TIFF', 'tif', 'tiff'):
- JPGi = open ("\\".join((path, fname)), 'rb')
- tags = EXIF.process_file(JPGi, stop_tag='Image DateTime')
- JPGi.close()
- if 'Image DateTime' in tags.keys(): # sufficient Exif data?
- imagedatetime = str(tags['Image DateTime']).split(" ")
- imagedate = imagedatetime[0].split(":")
- ifolder = imagedate # format: YYYYMMDD
- ifolder[0]=imagedate[0][2:4] # format: YYMMDD
- ifolderstr="".join(ifolder)
- notice = ""
- else: # using file properties: the earlier one of creation and modification date
- notice = ", using file properties (no EXIF)"
- (fnameMode,fnameIno,fnameDev,fnameNlink,fnameUID,fnameGID,fnameSize,fnameAcctime,fnameModtime,fnameCreattime) = os.stat(fname)
- ifolderstr = time.strftime('%y%m%d', time.localtime(min(fnameModtime,fnameCreattime)))
- if not os.path.exists("\\".join((path, ifolderstr))): # path already available?
- os.system(" ".join(("mkdir", ifolderstr)))
- print " ".join(("Created Folder:", ifolderstr))
- moveerror = os.system("".join(("move ", "\"", fname, "\" ", ifolderstr))) # NTFS move is much faster than copy + delete
- if moveerror is 0:
- print " ".join(("Moved", fname, "to", ifolderstr, notice))
- else:
- print " ".join(("Error moving", fname, "to", ifolderstr))
- # ignore everything else in this folder
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement