Organizing folders with Python and MacOS shortcuts

Abderraouf Benchoubane
5 min readJan 7, 2023

1. Introduction

Hi, my name is Abderraouf, and if you’re anything like me, your download folder looks something like Figure 01:

Figure 01: Messy download folder

A total mess with files everywhere. In this article, I will show you how you can organize it into something similar to Figure 02:

Figure 02: Organized folder

Much better, right?

2. What will you need?

  1. Python 3 is installed on your computer.
  2. Basic understanding of the fundamentals of programming.
  3. A code editor, I will use VSCode, but you can use whatever you want.
  4. A macOS computer is required if you want to use Siri Shortcuts to automate the execution of your script.

3. Let’s code

Create an empty folder and name it “Python folder cleaning project” inside the folder, initiate a Python environment and create a main.py script.

[Figure 03]: How to create a folder and file using the terminal

Now that you’ve made your project let’s look at the code in Figure 02 I grouped my files based on their types. For example, png and jpg are images. Pdfs and Docxs are documents. For the sake of learning, I recommend you follow the same grouping but feel free to change it to your liking afterwards.

In the project’s folder, create a JSON file name “file_formats” we will use this JSON file to map each file format to a specific folder. Mine is the following.


{
".mp3": "audios",
".mp4": "audios",
".jpg": "images",
".svg": "images",
".png":"images",
".jpeg": "images",
".HEIC": "images",
".pdf": "documents",
".docx": "documents",
".ods": "documents",
".zip": "archives",
".rar": "archives",
".tar.gz": "archives",
".gz": "archives",
".iso": "archives",
".dmg": "archives",
".csv": "data",
".odt":"spreadsheets",
".xlsx":"spreadsheets",
".fbx": "3d_assets",
".py": "scripts",
".sh": "scripts",
".json": "data",
".pptx": "presentations",
".txt": "data",
".mov": "videos",
".ipynb": "scripts"
}

In your main.py script, you will need to open this JSON file and parse it into a dictionary.

[Figure 05]: Parsing a JSON file

Now that we have mapped each file format to a folder name, we can declare a function name “clean_folder” that takes the “file_formats” dictionary and the path to the folder we want to clean. The code below showcases the “clean_folder” function.

[Figure 06]: Cleaning folder

Let’s look deeper into what is happening in Figure 06. We start by listing the content of the target_directory by calling:

dir_content = os.listdir(target_directory)

And now, for each item in the directory, we make sure that the item is not a folder using the following line:

if os.path.isdir(d_file):
continue # continue looping and ignore the current iteration

Another verification we have to do is if the file format is not in our JSON file (the one we declared earlier), then we ignore the file. We can achieve this by the following lines of code:

file_name, file_extension = os.path.splitext(d_file)

if not file_extension.lower() in file_formats:
continue

Great, now we are dealing with files we want to organize. To do so, we get the folder name we want to put the file into, and if the new folder does not exist, we create it. We can achieve this by adding the following lines of code:

target_path = f'{target_directory}/{file_formats[file_extension.lower()]}'
if not os.path.isdir(target_path):
os.mkdir(target_path)

All we have left is to move the file into the target_path and remove it from the original folder.

src_path = f'{target_directory}/{d_file}'
dst_path = f'{target_path}/{d_file}'

shutil.copyfile(src=src_path, dst=dst_path)
os.remove(src_path)

The complete source code should resemble something like this:

[Figure 07]: Full script for the download folder

4. Let’s make it more flexible

The script above does clean the Download folder, but what if you want to clean the Documents folder? Let’s not repeat ourselves. We can use arguments that we pass into the script. Add the red lines of code as detailed in Figure 08.

[Figure 08]: Using system arguments to clean user-specified folder

Now, you can navigate to your Python script folder and run the following command whenever you want to organize a folder.

python3 main.py folder_you_want_to_clean

5. [Extra] Running the script from Siri Shortcut

In 2021 Apple added the Siri shortcut application to macOS. Siri shortcuts allow you to automate tasks and run scripts with a button press.

Let’s see how we can add our script to a Siri shortcut.

First, open the Shortcuts app and click on the plus button next to the search bar, as shown in Figure 09.

[Figure 09]: Creating a new Siri Shortcut
[Figure 10]: Add shortcut screen

Once you’re in the add shortcut screen (Figure 10), make the following changes:

  1. Rename your shortcut to something meaningful to you. Mine has the two at the end because I have already added this script.
  2. Switch to the Apps tabs in the right panel.
  3. Scroll down the list and select the terminal.
  4. Double-click on “Run Shell Script.”

A new widget should appear with the details of the script. The text box with “echo “Hello world” is where we will type our shell script. Replace the echo command with the following:

cd /Users/your_user_name/path_to_the_python_script
python3 main.py path_to_folder_to_clean

It should resemble something like this:

[Figure 10]: Example of shortcut

You can press the play button and see your folder organized 🎉.

6. Thanks

That’s it for this article. I hope you found it interesting, and I hope you learned something from it. Feel free to comment on what you would like to see next. Thank you for your time.

7. Links.

If you want the full source code, here’s the Github link to this project.

--

--

Abderraouf Benchoubane

Hi ! my name is Abderraouf. I am a software engineering student at Polytechnique of Montreal