Want more features on Pastebin? Sign Up, it's FREE!
Guest

Python script for compressing files in batches (WITH CHECK)

By: a guest on Jul 17th, 2011  |  syntax: Python  |  size: 1.13 KB  |  views: 177  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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)
clone this paste RAW Paste Data