简体   繁体   中英

How do I move files into folders based on a part of their name?

I have a bunch of files which download from a server and I've searched this question on SO and none of the solutions work for me.

My files look like this:

date1_A.gz
date1_B.gz
date1_C.gz
date2_A.gz
date2_B.gz
date2_C.gz

I have two months worth for 10 variables. I want to define what these variables are (such as A, B, C) and push each file into a folder based on what it says in their name. So it would go from my downloads straight into a location with 10 folders where each folder has the files.

I tried to run a for loop which parsed the files and pushed them to a new location:

dir_name = "/Users/mee/Downloads/batch_files"

for subdir, dirs, files in os.walk(dir_name):
    for d in dirs:
        for file in files:
            the_file = os.path.join(subdir, file)
            if d in the_file:
                new_loc = subdir + '\\' + d + '\\' + file
                os.rename(the_file, new_loc)

Ideally, my files would go from my downloads into a directory I make which leads to a folder with 10 folders within it for each different data domain such as A, B, C.

I'd be tempted to construct a dictionary of regexes first, that you can use to categorise your filenamess:

import re

f_matcher = { "typeA" : re.compile("date.*A\.gz"), 
              "typeB" : re.compile("date.*B\.gz"), 
              "typeC" : re.compile("date.*C\.gz") }

If you're unsure about using regexes, try regex101 which is a great learning resource for this powerful pattern matching toolkit.

Then define a type-to-folder mapping:

f_folders = { "typeA" : "nas/folder/A", 
              "typeB" : "nas/folder/B", 
              "typeC" : "nas/folder/C" }

Define a function to perform the matching:

def match_name(class_d, name, default="typeX"):
    for c,r in f_matcher.items():
        if r.match(name):
            return c
    return default

And then, within your loop, extract your filename, and use a subloop or def to perform your tagging - and map that to your predefined list of paths:

f_type = match_name(f_matcher, filename)
new_path = f_folders.get(f_type, "/nas/default/")

Finally,make use of os.path.split and os.path.join to reconstruct the filenames you want for your files, and then either construct an os call to move them using os-commands, or using the kind of technique employed here .

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