Last active
July 1, 2021 20:06
-
-
Save moinologics/68d2b2e67fdc1c40b30c8867ec8ee9be to your computer and use it in GitHub Desktop.
finding large files in the system using python
This file contains hidden or 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
from os import walk, sep, path | |
from math import floor, log | |
def convert_size(size_bytes): | |
if size_bytes == 0: | |
return "0B" | |
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") | |
i = int(floor(log(size_bytes, 1024))) | |
p = 1024 ** i | |
s = round(size_bytes/p, 2) | |
return "%s %s" % (s, size_name[i]) | |
if __name__ == '__main__': | |
base_dir = input('Enter Base Directory: ') | |
max_size_mb = float(input('Enter Minimum FileSize(in MB): ')) | |
max_size_bytes = int(max_size_mb * 1048576) | |
for subdir, dirs, files in walk(base_dir): | |
for filename in files: | |
file_path = subdir + sep + filename | |
file_size_bytes = path.getsize(file_path) | |
if file_size_bytes >= max_size_bytes: | |
file_size = convert_size(file_size_bytes) | |
print(file_size+' '+file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment