The project outline:

It’s not uncommon for a few unneeded but humongous files or folders to take up the bulk of the space on your hard drive. If you’re trying to free up room on your computer, you’ll get the most bang for your buck by deleting the most massive of the unwanted files. But first you have to find them.

Write a program that walks through a folder tree and searches for exceptionally large files or folders—say, ones that have a file size of more than 100MB. (Remember that to get a file’s size, you can use os.path.getsize() from the os module.) Print these files with their absolute path to the screen.

My solution:

import send2trash
from pathlib import Path

def main():
    while True:
        basedir = Path(input("Please enter a folder to search: "))
        if not basedir.is_dir():
            print("This path does not exist.")
            for filename in basedir.rglob("*"):
                if filename.stat().st_size > 100000000:
                    print(f"{filename} is {filename.stat().st_size} bytes, sending to trash...")

if __name__ == '__main__':
  • 1
    \$\begingroup\$ Incorporating advice from an answer into the question violates the question-and-answer nature of this site. You could post improved code as a new question, as an answer, or as a link to an external site - as described in I improved my code based on the reviews. What next?. I have rolled back the edit, so the answers make sense again. \$\endgroup\$ Commented Oct 14, 2022 at 15:28

2 Answers 2

  • Write a program that walks through a folder tree and searches for exceptionally large files ...

    Your code only scans the single folder. It doesn't walk a folder tree.

  • ... or folders

    That is perhaps the trickiest part, and the problem statement is very unclear, perhaps intentionally. A size of a folder, as reported by stat() is usually very small. The task - likely - is to sum the sizes of files in the entire tree undeer a given folder. and report it if necessary.

  • But first you have to find them.

    The task is to find those files. Don't send2trash.send2trash them right away.

In any case, os.walk is your friend.

  • 1
    \$\begingroup\$ The OP used .rglob("*"), not .glob"*"), so it will recurse through subdirectories. It is not just scanning a single folder. \$\endgroup\$
    – AJNeufeld
    Commented Oct 15, 2022 at 5:56
  • Don’t put magic numbers in your code. Use constants (FILE_SIZE_THRESHOLD = 100 * 1024 * 1024) at the top of the module (below the imports).
  • 100 MB = 1024 * 1024 bytes, not 1000000 bytes. That’s a MiB.
  • You’re calling stat().st_size two times. Make a habit of creating a variable in such cases that contains the calculated / fetched value.
  • I see strings in Python most of the time enclosed in single quotes (‘), not in double quotes (“). Not sure if and where this is documented. PEP8 explicitly doesn’t take position in this matter (it just states that you should choose either one).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.