Last active
May 7, 2020 07:57
-
-
Save markheramis/c69cbb408363748f1f5790d64427fd87 to your computer and use it in GitHub Desktop.
Check directory content size (1 level only for files and directory)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, os, subprocess, math | |
args = sys.argv[1:] | |
#print 'Number of arguments:', len(args), 'arguments.' | |
#print 'Argument List:', str(args) | |
def get_size(start_path = '.'): | |
total_size = 0 | |
for dirpath, dirnames, filenames in os.walk(start_path): | |
for f in filenames: | |
fp = os.path.join(dirpath, f) | |
total_size += os.path.getsize(fp) | |
#print(total_size) | |
return total_size | |
def convert_size(size_bytes): | |
if size_bytes == 0: | |
return "0B" | |
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") | |
i = int(math.floor(math.log(size_bytes, 1024))) | |
p = math.pow(1024, i) | |
s = round(size_bytes / p, 2) | |
return "%s %s" % (s, size_name[i]) | |
def scan_path(path = ''): | |
size = 0 | |
try: | |
if os.path.exists(path): | |
if os.path.isfile(path): | |
# deal with file | |
size = os.stat(path).st_size | |
elif os.path.isdir(path) : | |
# deal with directory | |
size = get_size(path) | |
print "(" + convert_size(size) + ")", path | |
return size | |
else: | |
print("path does not exists") | |
except: | |
print "Can't read path", path | |
for path in args: | |
contents = os.listdir(path) | |
total_size = 0 | |
for val in contents: | |
val = path + "/" + val | |
size = scan_path(val) | |
if size : | |
total_size += scan_path(val) | |
print "Total Size:", convert_size(total_size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Objective is to scan a directory to find out which one is taking up too much space.