Coding

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.

 

[sourcecode language=”python” wraplines=”false” collapse=”false”]

#Usage python big.py limit in MB <source>

import sys, os

if len(sys.argv) >= 2:
limit = sys.argv[1]
source = sys.argv[2] if len(sys.argv) == 3 else 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) / 1000000
if MB > int(limit):
print(filepath)

[/sourcecode]

Please follow and like us:
0

Enjoy this blog? Please spread the word :)

error: Content is protected !!