From 39d78c8c3d2aae752c19d8db46fa09c5b3bba10e Mon Sep 17 00:00:00 2001 From: tfrdidi Date: Sun, 6 Dec 2015 03:17:15 +0100 Subject: [PATCH] check for file existance before moving, deleting if already existing -> faster when restarting --- jpgSorter.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/jpgSorter.py b/jpgSorter.py index ce278e7..467086f 100644 --- a/jpgSorter.py +++ b/jpgSorter.py @@ -1,4 +1,5 @@ import os.path +import ntpath import exifread from time import localtime, strftime, strptime, mktime import shutil @@ -72,13 +73,18 @@ def writeImages(images, destinationRoot): today = strftime("%d/%m/%Y") for imageTuple in sortedImages: + destination = "" + destinationFilePath = "" t = localtime(imageTuple[0]) year = strftime("%Y", t) creationDate = strftime("%d/%m/%Y", t) + fileName = ntpath.basename(imageTuple[1]) + if(creationDate == today): createUnknownDateFolder(destinationRoot) destination = os.path.join(destinationRoot, unknownDateFolderName) - shutil.move(imageTuple[1], destination) + destinationFilePath = os.path.join(destination, fileName) + else: if (previousTime == None) or ((previousTime + minEventDelta) < imageTuple[0]): previousTime = imageTuple[0] @@ -91,9 +97,15 @@ def writeImages(images, destinationRoot): # it may be possible that an event covers 2 years. # in such a case put all the images to the even in the old year if not (os.path.exists(destination)): - destination = os.path.join(destinationRoot, year-1, str(eventNumber)) + destination = os.path.join(destinationRoot, str(int(year) - 1), str(eventNumber)) + destinationFilePath = os.path.join(destination, fileName) + + if not (os.path.exists(destinationFilePath)): shutil.move(imageTuple[1], destination) + else: + if (os.path.exists(imageTuple[1])): + os.remove(imageTuple[1]) def postprocessImages(imageDirectory):