How I Organise My Downloads Folder With Python

automation feed
Aug 28, 2019 · 4 min read
Image for post
Image for post
Photo by Andrew Pons on Unsplash

Recently, I felt overly annoyed by my messy download folder on my personal computer. There were too many files, and it was in a total mess. Being someone with mild OCD, I was annoyed. I spent an afternoon trying to think about a solution, and I thought maybe I could write a simple Python script to address this problem.

Image for post
Image for post
This is my download folder

The Solution

The solution a Python script that loops the download folder and determines the file type for each and every file. Subsequently, it creates a folder for that file type. All files with the same file type will be transferred to this folder. This script should work whenever a file gets added to the downloads folder.

Suppose we have a download folder that looks like this:

Image for post
Image for post
Mixture of JPG, PNG, and TXT files

This is the result that we hope to achieve:

Image for post
Image for post
YThe download folder is sorted according to type

Upon closer inspection, for example in the txt_folder, we should be expecting something like this:

Image for post
Image for post
Yes!! All my text documents are in txt_folder

Now let’s delve in and look at the code:

from os import listdir
from os.path import isfile, join
import os
import shutil
def sort_files_in_a_folder(mypath): '''
A function to sort the files in a download folder
into their respective categories
'''
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
file_type_variation_list=[]
filetype_folder_dict={}
for file in files:
filetype=file.split('.')[1]
if filetype not in file_type_variation_list:
file_type_variation_list.append(filetype)
new_folder_name=mypath+'/'+ filetype + '_folder'
filetype_folder_dict[str(filetype)]=str(new_folder_name)
if os.path.isdir(new_folder_name)==True: #folder exists
continue
else:
os.mkdir(new_folder_name)
for file in files:
src_path=mypath+'/'+file
filetype=file.split('.')[1]
if filetype in filetype_folder_dict.keys():
dest_path=filetype_folder_dict[str(filetype)]
shutil.move(src_path,dest_path)
print(src_path + '>>>' + dest_path)if __name__=="__main__":
mypath='your_path_to_your_download_folder'
sort_files_in_a_folder(mypath)

Firstly, run these two commands on your terminal (Mac) or command prompt (Windows):

pip install os
pip install shutil

OS is a Python library that allows you to interact with the operating system, while shutil is another library that helps you in transferring files/folders/documents within directories.

files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
file_type_variation_list=[]
filetype_folder_dict={}
for file in files:
filetype=file.split('.')[1]
if filetype not in file_type_variation_list:
file_type_variation_list.append(filetype)
new_folder_name=mypath+'/'+ filetype + '_folder'
filetype_folder_dict[str(filetype)]=str(new_folder_name)
if os.path.isdir(new_folder_name)==True: #folder exists alrd
continue
else:
os.mkdir(new_folder_name)

Step-by-step tutorial

First, we create a list (files) that contains all the names of the files in a directory (e.g., downloads). We loop through this list and create a folder named according to the file type (e.g., json_folder), as long as it does not exist beforehand. Also, a dictionary, (filetype_folder_dict), will be created, which maps the key file type to a value.

for file in files:
src_path=mypath+'/'+file
filetype=file.split('.')[1]
if filetype in filetype_folder_dict.keys():
dest_path=filetype_folder_dict[str(filetype)]
shutil.move(src_path,dest_path)
print(src_path + '>>>' + dest_path)

Then, we loop the initial list again and move the file to its destination folder that we created previously. The destination path is determined by referring to the dictionary created earlier.

Lastly, we need to decide on how are we going to trigger this script. The ideal situation is that this script will be triggered whenever a new item gets added to the download folder; however, for me, I find the easier method is to create a crontab (Mac) or task scheduler (Windows).

These two programs allow Python scripts to be executed on a particular time of the day (and they can be controlled by the user).

Alternatively, you can just run the script manually whenever a few new items get added into the download folder … but that will be extremely tedious.

Link to crontab tutorial:

Link to window task scheduler tutorial:

Link to source code:

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store