简体   繁体   中英

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?

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM