My solution to the Practice Project #2 in Chapter 9 of the excellent book “Automate the Boring Stuff with Python” (ABSP)
#! usr/bin/env python3 # by lorenzo - 21/09/2017 # deleting unneeded files - program that walks through a folder tree # and searches for files that have a file size # larger than some threshold determined by the user, # tells the user where these files are and asks # whether the user wants to remove them or not. import os, sys, send2trash print("Enter location where you want to search for large files:") location = input() if not os.path.isdir(location): print("This directory does not exist!") sys.exit() print("Enter threshold (in bytes) for files size:") threshold = int(input()) choice = "" for folderName, subfolders, filenames in os.walk(location): for filename in filenames: if os.path.getsize(os.path.join(folderName, filename)) >= threshold: print("Found file with size over threshold:" + os.path.join(folderName, filename)) print("Do you want to delete it? (y/n)") choice = input() if choice == "y": send2trash.send2trash(os.path.join(folderName, filename)) else: continue
Comments about the code are welcome.