from time import sleep import os from subprocess import Popen, PIPE path=r"/storeM/fat/screenshots/active/" archiveFilename = r'/storeM/fat/screenshots/7z/test3.7z' #ensure path string ends in '/' if path[len(path)-1] != '/': path = path + r'/' 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 def checkCommand(cmd): p = Popen(cmd, stdout=PIPE, shell=True) p.wait() result = p.communicate()[0] assert 'Everything is Ok' in result for batch in splitBatches: batchOfFilenames = str(splitBatches[batch]).replace(',','').replace("'",'').replace('"','').replace('[','').replace(']','') #print batchOfFilenames checkCommand(r'7za a '+ archiveFilename +r' '+ batchOfFilenames) os.system('rm '+ batchOfFilenames)