Skip to content

Instantly share code, notes, and snippets.

@sethdavis512
Created August 12, 2017 04:58
Show Gist options
  • Save sethdavis512/b7b5f22a606d30c4a036cd89a5ddcb17 to your computer and use it in GitHub Desktop.
Save sethdavis512/b7b5f22a606d30c4a036cd89a5ddcb17 to your computer and use it in GitHub Desktop.
import os
import math
import csv
def humanizeFileSize(filesize):
filesize = abs(filesize)
if (filesize==0):
return "0 Bytes"
p = int(math.floor(math.log(filesize, 2)/10))
return "%0.2f %s" % (filesize/math.pow(1024,p), ['Bytes','KB','MB','GB','TB','PB','EB','ZB','YB'][p])
def csv_writer(data, path):
with open(path, "wb") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerow(['Name', 'Size', 'Label'])
for line in data:
writer.writerow(line)
targetDirectory = '/your/directory/name'
output = []
path = targetDirectory.split('/')[-1] + '-size-report.csv'
for root, dirs, files in os.walk(targetDirectory, topdown=False):
for name in files:
if (name not in ['.DS_Store']):
filePath = os.path.join(root, name)
fileSize = os.path.getsize(filePath)
size = humanizeFileSize(fileSize).split(' ')[0]
label = humanizeFileSize(fileSize).split(' ')[1]
output.append([name, size, label])
csv_writer(output, path)
print "File Successfully Written!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment