limit number of files/directory with subdirectories
This commit is contained in:
parent
330ceeece1
commit
525537c62a
|
@ -0,0 +1,27 @@
|
|||
import sys
|
||||
import math
|
||||
import os
|
||||
import shutil
|
||||
|
||||
|
||||
def limitFilesPerFolder(folder, maxNumberOfFilesPerFolder):
|
||||
for root, dirs, files in os.walk(folder, topdown=False):
|
||||
for dir in dirs:
|
||||
dirPath = os.path.join(root, dir)
|
||||
filesInFolder = len(os.listdir(dirPath))
|
||||
if(filesInFolder > maxNumberOfFilesPerFolder):
|
||||
numberOfSubfolders = math.ceil(filesInFolder / maxNumberOfFilesPerFolder)
|
||||
for subFolderNumber in range(1, numberOfSubfolders+1):
|
||||
subFolderPath = os.path.join(dirPath, str(subFolderNumber))
|
||||
if not os.path.exists(subFolderPath):
|
||||
os.mkdir(subFolderPath)
|
||||
fileCounter = 0
|
||||
for file in os.listdir(dirPath):
|
||||
source = os.path.join(dirPath, file)
|
||||
if os.path.isfile(source):
|
||||
destDir = str(math.floor(fileCounter / maxNumberOfFilesPerFolder)+1)
|
||||
destination = os.path.join(dirPath, destDir, file)
|
||||
shutil.move(source, destination)
|
||||
fileCounter += 1
|
||||
|
||||
|
Loading…
Reference in New Issue