import os path="/storeM/fat/screenshots/active/" archiveFilename = '/storeM/fat/screenshots/7z/test2.7z' #ensure path string ends in '/' if path[len(path)-1] != '/': path = path + '/' dirList=os.listdir(path) maxStackSize = 20 splitBatches = {} batchStack = 0 currentStack = [] currentStackNumber = 0 #let's make some batches of a given size (ignore anything greater for now) for file in dirList: if batchStack < maxStackSize: batchStack = batchStack + 1 currentStack.append(path + file) else: batchStack = 0 splitBatches[currentStackNumber]=currentStack currentStack = [] currentStackNumber = currentStackNumber + 1 for batch in splitBatches: batchOfFilenames = str(splitBatches[batch]).replace(',','').replace("'",'').replace('"','').replace('[','').replace(']','') os.system('7za a '+ archiveFilename +' '+ batchOfFilenames) os.system('rm '+ batchOfFilenames)