Scheduled Download Folder Cleanup with Python

Since diving back into Python some time ago, I’ve been coming up with scripts to make my life easier. One of my first creations is this very short and easy script which clears out my “Downloads” folder every evening at 7 PM. Here I’ll run through this simple¬†five line¬†script and explain it. Let’s get started!

 

If you’re anything like me your “Downloads” folder gets cluttered fast. Every few weeks I’ll look at it and realize I have gigs of unused files sitting there and delete everything. Python can do this automatically, every day. The first step in doing this is installing Python, and a program to write code in, which can be as simple as Window’s notepad. Head over to Python.org to get Python3 (the latest version).

 

Okay, now you’re all ready to go! Open up your text editor and lets write some code. The completed script looks like this:

 

import os
import glob
fileFolder = glob.glob('C:/Users/YourUsername/Downloads/*')
for file in fileFolder:
    os.remove(file)

 

Very basic stuff. First you’re importing the os and glob modules for use in this script. Next, the fileFolder variable is set to equal a list of every file in the specified directory, provided by using the glob module with the ‘*’ wildcard, meaning it will return every single file it finds. The ‘for’ loop iterates through each item (or ‘file’ based on our loop construction above) in the list ‘fileFolder’. Finally, for every file iterated through in ‘fileFolder’ the os.remove command deletes that file.

 

Great! It works! Save it somewhere and give it a .py file extension. I would suggest saving it somewhere out of the way, I put mine in my Python installation folder. Now how do we get it to happen automatically?

 

On Windows 8, and I believe other versions, there is a program called “Task Scheduler” which is exactly what we want to use. Press start, then search for it, and open it. Next we’ll click the “Create Basic Task” option which will ask you to name the task and provide a description. Name it something you’ll remember just in case you have to come back later and edit it. Click next, then tell it how often you’d like it to run and click next again, and specify what time it should run. In the Action screen select “Start a program”.

 

Now you need to specify which program to open. This is NOT your script. Instead, navigate to your Python installation directory and select the “python.exe” file inside. Mine is “C:\Python34\python.exe” which I believe is the default. After selecting that, fill in the “Add arguments (optional)” box with the path to your script, enclosed in quotations. For example, mine looks like this: “C:\Python34\downloads_maint.py”. Click next and finish, and enjoy your impeccably Python cleaned Downloads folder!

 

Obviously this is about as simple as a useful script can get, but now that you know how to schedule and write a little Python, you can scale that up to do much more useful things automatically. Drop me a line at osentand(at)gmail.com if you have any questions!