DEV Community

Cover image for Day 14 - List largest files - 100 days 100 python scripts
Ganesh Raja
Ganesh Raja

Posted on

Day 14 - List largest files - 100 days 100 python scripts

Day 14: list_large_files

This script will print the n number of biggest files in the specified dir and it's sub dir. It will be helpful when you want to free up some space in HD

import os,constants
import math
from collections import Counter

LOCATION=constants.DOWNLOAD_PATH
LIST_NUMBER_OF_FILES=25
FILE_SIZE=6250000 #Approx 50 MB

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])


files=[val for sublist in [[os.path.join(i[0], j) for j in i[2]] for i in os.walk(LOCATION)] for val in sublist]


files=list(filter(lambda x: os.stat(x).st_size >FILE_SIZE,files))
c = Counter()
for f in files:
    c[f]=os.stat(f).st_size

for item in (sorted(c.items(), key=lambda i: i[1], reverse=True)[:]):
    print(os.path.split(item[0])[1]," - ",convert_size(item[1]))

Please Visit my Git Repo to check out all the previous day challenges.

https://github.com/ganeshraja10/automated-python-scripts

Discussion (0)