Skip to content

Blog

File Sorter (Part 2): Functions, Dry-Run, and Safe Duplicates

In my previous post, I built a basic Python script to clean up a cluttered Downloads folder. It did the job, but it was just a procedural script with clear risks: it could overwrite files with duplicate names, and running it meant making changes directly to the filesystem without a preview.

As someone training to become a QA engineer, I realized this was not reliable enough. Before writing automated tests, code must be modular and predictable.

Here is how I upgraded the script to make it safer and test-ready.


What Changed?

1. Modular Functions (Preparing for Pytest)

In the first version, everything ran in a single loop. To test logic effectively, inputs and outputs need to be isolated. I split the logic into dedicated functions:

  • classify_file(): pure logic that only determines the category.
  • get_unique_path(): handles file naming collisions.
  • move_file(): handles the actual filesystem operation.
  • sort_directory(): coordinates the entire workflow.

2. Collision Handling: No Overwrites

If you download report.pdf multiple times, standard move operations might overwrite older files. I added get_unique_path() to append an incrementing index if a file already exists in the target folder:

def get_unique_path(destination: Path) -> Path:
    if not destination.exists():
        return destination
    counter = 1
    while True:
        new_filename = (
            f"{destination.stem}_{counter}{destination.suffix}"
        )
        new_destination = destination.parent / new_filename
        if not new_destination.exists():
            return new_destination
        counter += 1

Now, report.pdf safely becomes report_1.pdf, protecting user data.

File Sorter (Part 1): Automating File Organization with Python

The "Downloads" folder inevitably turns into a "black hole" where documents, archives, and media blend into an endless list. To avoid wasting time on manual sorting, I developed a simple Python script to handle the heavy lifting.

This exercise allowed me to practice filesystem operations and bring a bit more order to my Linux Mint environment.

Project Source

The full implementation is available on GitHub: vvsparrow/file-sorter

Constructive feedback and suggestions are always appreciated as I continue my Python journey.

Linux Downloads folder full of cluttered files before automated
sorting Linux Downloads folder full of cluttered files before automated sorting.

My Technical Workflow

I utilized the pathlib library for a modern approach to path manipulation and shutil for secure file transfers. The script analyzes file extensions and assigns them to predefined categories.

Key Features:

  • Multi-part Extension Support: The script correctly handles files like .tar.gz instead of just looking at the final suffix.
  • Safety First: If a category folder doesn't exist, the script creates it automatically using mkdir(exist_ok=True).

Overcoming Learning Barriers: Why I Build My Own Offline Library

Currently, I am based in Russia, where accessing global educational resources like YouTube has become a significant challenge due to state-imposed restrictions (RKN blocks). For a student, this is more than an inconvenience — it’s a barrier to professional growth.

Every other day, I spend about 3 hours commuting to the gym and training. I refused to let this time go to waste. To ensure my English learning remains uninterrupted despite unstable connectivity and censorship, I developed a reliable offline workflow using yt-dlp.

yt-dlp processing a 100-video playlist Automating my offline English library: processing an entire YouTube playlist via terminal.

My Technical Workflow

I don’t just "download videos"; I automate the creation of high-quality educational audio files that I can listen to anywhere, regardless of the current state of the local network.