ABSP Ch 9, Practice Project #2

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!")

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

Comments about the code are welcome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s