Python Learning: Automate Boring Stuff with Python | Chapter 8 : My Solution to Practice Project: Deleting Unneeded Files

Personally, one of the simplest projects yet

This code (when properly indented) helps to list out all files with a size greater than the given limit.

 

# Usage python big.py limit in MB 

import sys
import os

if len(sys.argv) >= 2:
    limit = sys.argv[1]
    if len(sys.argv) == 3:
        source = sys.argv[2]
    else:
        source = os.getcwd()
    source = os.path.abspath(source)
    if not os.path.exists(source):
        print("Provide valid source folder")
    else:
        for folder, subfolders, files in os.walk(source):
            for file in files:
                filepath = os.path.join(folder, file)
                MB = os.path.getsize(filepath)
                if MB >= int(limit):
                    print("File <" + filepath + "> 's size : " + str(MB) + " bytes is larger or equals to " + str(limit) + " bytes")
                else:
                    print("File <" + filepath + ">'s size : " + str(MB) +
                          " bytes is smaller than " + str(MB) + " bytes")
        print("Complete Analysis")
else:
    print("Usage: python big.py  ")