process images only afterwards, log method, limitation of files per directory

This commit is contained in:
tfrdidi 2015-12-06 01:53:14 +01:00
parent 71791236b9
commit 330ceeece1
1 changed files with 38 additions and 23 deletions

View File

@ -2,21 +2,28 @@
import os import os
import os.path import os.path
import sys import sys
import jpgSorter import jpgSorter, numberOfFilesPerFolderLimiter
import time
import shutil import shutil
from time import gmtime, strftime from time import localtime, strftime
import math
def getFolderSizeInGb(start_path = '.'): def getNumberOfFilesInFolderRecursively(start_path = '.'):
total_size = 0 numberOfFiles = 0
for dirpath, dirnames, filenames in os.walk(start_path): for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames: for f in filenames:
fp = os.path.join(dirpath, f) fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp) if(os.path.isfile(fp)):
return int(total_size / 1024 / 1024 / 1024) numberOfFiles += 1
return numberOfFiles
def getNumberOfFilesInFolder(path):
return len(os.listdir(path))
def log(logString):
print(strftime("%H:%M:%S", localtime()) + ": " + logString)
maxNumberOfFilesPerFolder = 500
source = None source = None
destination = None destination = None
@ -28,32 +35,40 @@ else:
destination = sys.argv[2] destination = sys.argv[2]
print("Destination directory: " + destination) print("Destination directory: " + destination)
if(len(sys.argv) > 3):
maxNumberOfFilesPerFolder = int(sys.argv[3])
while ((source is None) or (not os.path.exists(source))): while ((source is None) or (not os.path.exists(source))):
source = input('Enter a valid source directory\n') source = input('Enter a valid source directory\n')
while ((destination is None) or (not os.path.exists(destination))): while ((destination is None) or (not os.path.exists(destination))):
destination = input('Enter a valid destination directory\n') destination = input('Enter a valid destination directory\n')
totalAmountToCopy = str(getFolderSizeInGb(source)) totalAmountToCopy = str(getNumberOfFilesInFolderRecursively(source))
print("Files to copy: " + totalAmountToCopy + " GB.") print("Files to copy: " + totalAmountToCopy)
images = []
fileCounter = 0
for root, dirs, files in os.walk(source, topdown=False): for root, dirs, files in os.walk(source, topdown=False):
print (strftime("%H:%M:%S", gmtime()) + ": " + str(getFolderSizeInGb(destination)) + " / " + totalAmountToCopy + " GB processed.")
for file in files: for file in files:
extension = os.path.splitext(file)[1][1:].upper() extension = os.path.splitext(file)[1][1:].upper()
path = os.path.join(root,file) sourcePath = os.path.join(root, file)
destinationPath = os.path.join(destination, extension) destinationDirectory = os.path.join(destination, extension)
if not os.path.exists(destinationPath): if not os.path.exists(destinationDirectory):
os.mkdir(destinationPath) os.mkdir(destinationDirectory)
if extension == "JPG": fileName = str(fileCounter) + "." + extension.lower()
jpgSorter.postprocessImage(images, root, destinationPath, file) destinationFile = os.path.join(destinationDirectory, fileName)
else: if not os.path.exists(destinationFile):
if os.path.exists(os.path.join(destinationPath, file)): shutil.copy(sourcePath, destinationFile)
shutil.copy(os.path.join(root,file), os.path.join(destination, extension, str(time.time()) + file))
else:
shutil.copy(os.path.join(root,file), destinationPath)
jpgSorter.writeImages(images, os.path.join(destination, "JPG")) fileCounter += 1
if((fileCounter % 100) is 0):
log(str(fileCounter) + " / " + totalAmountToCopy + " processed.")
log("start special file treatment")
jpgSorter.postprocessImages(os.path.join(destination, "JPG"))
log("assure max file per folder number")
numberOfFilesPerFolderLimiter.limitFilesPerFolder(destination, maxNumberOfFilesPerFolder)