surrexitt
surrexitt

Reputation: 41

Organize files by name into categorical subfolders in Python

I need an algorithm that organize some files in a directory by the name of the file.

I wrote some categories:

Bread = ["bread", "pizza", "spaghetti"]
Meat = ["hamburger", "meat", "porkchop"]

If a file name is hamburger recipe.txt, I need this file to be moved to a particular directory called Meat.

If another file name is bread with vegetables.doc, this file will be moved to the folder named Bread.

I tried to write this, but it doesn't work:

meat = ["hamburger", "meat", "porkchop"]

for filename in os.listdir(path):
    if meat in filename:
        os.rename(filename, "Meat/" + filename)

Can you help me?

Upvotes: 2

Views: 830

Answers (2)

ggorlen
ggorlen

Reputation: 56885

This is the right idea. I'd use a dictionary to make categories easier to manipulate. While it makes sense to map categories to keywords for organizational purposes, lookups will be faster by inverting the dict. At that point, we can split each filename on non-word characters, check our keyword lookup table for a match, create any nonexistent directories and move files as necessary.

import os
import re

path = "."
categories = {
    "meat": ["hamburger", "meat", "porkchop"],
    "bread": ["bread", "pizza", "spaghetti"]
} 
keywords = {}

for k, v in categories.items():
    for x in v:
        keywords[x] = k

for filename in [x for x in os.listdir(path) if os.path.isfile(x)]:
    for term in [x for x in re.split(r"\W+", filename.lower()) if x in keywords]:
        dest = os.path.join(keywords[term], filename)
        src = os.path.join(path, filename)

        try:
            if not os.path.exists(keywords[term]):
                os.makedirs(keywords[term])

            os.rename(src, dest)
        except (FileNotFoundError, FileExistsError) as e:
            print(e)

Upvotes: 1

Thierry Lathuille
Thierry Lathuille

Reputation: 24232

You have to test if any of the food items in your meat category appears in the filename:

meat = ["hamburger", "meat", "porkchop"]

for filename in os.listdir(path):
    if any(food in filename for food in meat):
        os.rename(filename, "Meat/" + filename)

Upvotes: 1

Related Questions