This simple simple text program will finally repair the chaotic download folder

As a Python developer that spends half of my life data, I am everything about automation. So when you stumbled through Downloads-ice tools On GitHub, a stylish small text program that regulates your Windows download folder, I knew I found a jewel. Best yet? It was open, and I can make it for me.

In my job, mostly I score Pandas and Numby to analyze data, with some javascript on the side. This project spoke to me: A clean symbol, a problem in the real world, and an opportunity to contribute. My mission? Add support to rtfand odtAnd ods Files-performance I often escalate, especially those OpenDocument of data disposal. Here is how I jumped into the open source, made my mark, and turned a chaotic folder into a rank.

The project: the cleaning crew that works in Bethon

First, let’s disintegrate what Downloads-ice tools He does. It is a Python text program that wipes the download folder, reads the Config.json file to set file accessories to sub -folders (such as “documents” or “photos”), and moves everything he belongs to. Think about. PDF files sliding into “documents”. It is simple and elegant and provides you with awe of manual sorting.

The composition file is the brain’s brain. Here’s a peek on the version I worked with, including my additions:

[
  {
    "name": "Documents",
    "extensions": [
      ".doc",
      ".docx",
      ".txt",
      ".pdf",
      ".xls",
      ".ppt",
      ".xlsx",
      ".pptx",
      ".rtf",
      ".odt",
      ".ods"
    ]
  },
  {
    "name": "Pictures",
    "extensions": [
      ".jpg",
      ".jpeg",
      ".png",
      ".svg",
      ".gif",
      ".tif",
      ".tiff"
    ]
  },
  // ... other categories like "Programs," "Music," etc.
]

The same text program uses Python’s pathlib To deal with the path, shutil To transfer files, and Json to analyze this composition. It flying through the files, achieves their extensions, and creates them to the right place. If there is no folder (for example, “documents”), it creates it.

Add more file types

So why do you modify it? As a data analyst, deal with.ods (OpenDocument) files all the time – make a decision with a Libreofice answer on Excel. and .odt (OpenDocument) and .rtf (Poisoning a rich text) floats on the surface in my world as well. The original composition covered a lot –.pdfand .mp3and .zip– But I missed this. I was counted, why don’t you make it more comprehensive?

Here is how it fell:

  1. Fork and cloning: I am a dilemma eric-mahasi/downloads-folder-automation To my Github, cloning it locally, and opening it. GIT is still a little wild monster for me, but I managed!

  2. Editing configuration: in config.jsonI added .rtfand .odtAnd .ods To the category of “documents”. The logic spot – Spreadsheets can go to another place, but I kept it simple.

  3. TestI have been delivered some test files in the download folder – .odt Note, and .ods Data set, random .rtf. The scenario ran (python main.py) And BAM – joined a new “document” sub -volume.

  4. Request withdrawal: My changes have subsided (git checkout -b add-more-extensions), Commit them (git commit -m "Add rtf, odt, ods to config), I pushed my fork, and opened public relations. Describing it like: “Add support to .Tf,. “Now I am waiting for Eric notes.

To give my rotation on the symbol – and avoid just avoiding the original – here how I write it today:

import shutil
from pathlib import Path
import json


def relocate_file(source_file, target_dir):
    """Moves a file to a specified folder, creating the folder if it doesn’t exist.
    
    Args:
        source_file (Path): Path to the file being moved
        target_dir (Path): Path to the destination directory
    """
    try:
        if not target_dir.exists():
            target_dir.mkdir(parents=True, exist_ok=True)
        shutil.move(source_file, target_dir)
    except shutil.Error as error:
        print(f"Error moving file: {error}")


def organize_directory(directory_path):
    """Sorts files in a directory into subfolders based on their extensions.
    
    Args:
        directory_path (Path): Path to the directory to organize
    """
    with open('config.json', encoding='utf-8') as config_file:
        file_groups = json.load(config_file)

    extension_to_folder = {}
    for group in file_groups:
        folder = group['name']
        for ext in group['extensions']:
            extension_to_folder[ext] = folder

    for item in directory_path.iterdir():
        if item.is_file() and not item.name.startswith('.'):
            target_folder = extension_to_folder.get(item.suffix, 'Miscellaneous')
            relocate_file(item, directory_path / target_folder)


if __name__ == '__main__':
    user_home = Path.home()
    downloads_dir = user_home / 'Downloads'

    organize_directory(downloads_dir)

Try it yourself

Do you want to tame your download folder or any folder? Ribo thorns, seize the updated Config.json, run the text program. Add your file types – perhaps .py For “text programs” or .csv For “data”? It is for you to hack. And if you are new to open the source, this is a perfect sand box – useful, and wicked.

Leave a Comment