DEV Community

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

Posted on

2 1

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]))
Enter fullscreen mode Exit fullscreen mode

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

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More