SHARE
TWEET

Untitled

a guest Jan 18th, 2019 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. from subprocess import Popen, PIPE
  3.  
  4. def getFolderSize(folder):
  5.   total_size = os.path.getsize(folder)
  6.   for item in os.listdir(folder):
  7.     itempath = os.path.join(folder, item)
  8.     if os.path.isfile(itempath):
  9.       total_size += os.path.getsize(itempath)
  10.       if os.path.isdir(itempath):
  11.         total_size += getFolderSize(itempath)
  12.   return total_size
  13.  
  14. def compress(output_file, directory):
  15.   process = Popen(["C:/Program Files/7-Zip/7z.exe", "a", output_file, directory+os.path.sep+"*"], stdout=PIPE)
  16.   process.communicate()
  17.   exit_code = process.wait()
  18.   if (exit_code != 0):
  19.     print ("Copression of " + directory + " failed")
  20.   else:
  21.     print ("Copressed " + directory + " to " + output_file)
  22.  
  23.  
  24.  
  25. rootdir = "C:/temp/test/"
  26.  
  27. for root,dirs,files in os.walk(rootdir, topdown=True):
  28.     depth = root[len(rootdir) + len(os.path.sep):].count(os.path.sep)
  29.     if depth > 3:
  30.       dirs[:] = [] # Don't recurse any deeper
  31.       continue
  32.  
  33.     for dir in dirs:
  34.       if "archive" in dir:
  35.         path = os.path.join(root, dir)
  36.         size = getFolderSize(path)
  37.         print (path, size)
  38.         if size >= 1024: #bytes
  39.           compress(root + os.path.sep + "archive.7z", path)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top