Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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:
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