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.")
continue
else:
for filename in basedir.rglob("*"):
if filename.stat().st_size > 100000000:
print(f"{filename} is {filename.stat().st_size} bytes, sending to trash...")
send2trash.send2trash(filename)
if __name__ == '__main__':
main()