Advertisement
Guest User

Python script for compressing files in batches (WITH CHECK)

a guest
Jul 17th, 2011
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. from time import sleep
  2. import os
  3. from subprocess import Popen, PIPE
  4. path=r"/storeM/fat/screenshots/active/"
  5. archiveFilename = r'/storeM/fat/screenshots/7z/test3.7z'
  6. #ensure path string ends in '/'
  7. if path[len(path)-1] != '/':
  8.     path = path + r'/'
  9.  
  10. dirList=os.listdir(path)
  11.  
  12. maxStackSize = 20
  13. splitBatches = {}
  14. batchStack = 0
  15. currentStack = []
  16. currentStackNumber = 0
  17.  
  18. #let's make some batches of a given size (ignore anything greater for now)
  19. for file in dirList:
  20.     if batchStack < maxStackSize:
  21.         batchStack = batchStack + 1
  22.         currentStack.append(path + file)
  23.     else:
  24.         batchStack = 0
  25.         splitBatches[currentStackNumber]=currentStack
  26.         currentStack = []
  27.         currentStackNumber = currentStackNumber + 1
  28.        
  29.  
  30. def checkCommand(cmd):
  31.     p = Popen(cmd, stdout=PIPE, shell=True)
  32.     p.wait()
  33.     result = p.communicate()[0]
  34.     assert 'Everything is Ok' in result
  35.  
  36. for batch in splitBatches:
  37.     batchOfFilenames = str(splitBatches[batch]).replace(',','').replace("'",'').replace('"','').replace('[','').replace(']','')
  38.     #print batchOfFilenames
  39.     checkCommand(r'7za a '+ archiveFilename +r' '+ batchOfFilenames)
  40.     os.system('rm '+ batchOfFilenames)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement