Advertisement
TheProfosist

Directory Listing with size (no sub folders) via Python 08

Jul 1st, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import sys
  3.  
  4. def get_size(start_path = '.'):
  5.     total_size = 0
  6.     for dirpath, dirnames, filenames in os.walk(start_path):
  7.         for f in filenames:
  8.             fp = os.path.join(dirpath, f)
  9.             total_size += os.path.getsize(fp)
  10.  
  11.     if total_size == 0 and os.path.isfile(start_path):
  12.         total_size += os.path.getsize(start_path)
  13.  
  14.     return total_size
  15.  
  16. def listContents(start_path = '.'):
  17.     print('running on dir "' + start_path + '"')
  18.     dirName = os.path.basename(os.path.abspath(start_path))
  19.  
  20.     contents = os.listdir(start_path)
  21.     f = open('output_' + dirName + '.csv', 'w')
  22.     for c in contents:
  23.         str(c.encode('utf8'))
  24.         f.write(', ')
  25.         f.write(str(get_size(start_path + '/' + c) / 1024 / 1024/ 1024) + '\n')
  26.     f.close();
  27.  
  28. if len(sys.argv) > 1:
  29.     listContents(sys.argv[1])
  30. else:
  31.     listContents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement