简体   繁体   English

如何根据文件名称的一部分将文件移动到文件夹中?

[英]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. 我有一堆文件是从服务器下载的,我已经在SO上搜索了这个问题,但没有一种解决方案适合我。

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. 我有两个月的时间来购买10个变量。 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. 我想定义这些变量是什么(例如A,B,C),然后根据其名称将每个文件推送到一个文件夹中。 So it would go from my downloads straight into a location with 10 folders where each folder has the files. 因此,它将从我的下载直接进入一个包含10个文件夹的位置,其中每个文件夹都有文件。

I tried to run a for loop which parsed the files and pushed them to a new location: 我试图运行一个for循环,该循环分析文件并将其推到新位置:

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. 理想情况下,我的文件将从下载的文件进入我创建的目录,该目录会指向一个文件夹,其中每个A,B,C等不同的数据域都有10个文件夹。

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. 如果不确定使用正则表达式,请尝试regex101 ,它是此功能强大的模式匹配工具包的绝佳学习资源。

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: 然后,在循环中,提取文件名,并使用子循环或def执行标记-并将其映射到预定义的路径列表:

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 . 最后,利用os.path.splitos.path.join重建文件所需的文件名,然后构造一个os调用以使用os-commands或此处采用的某种技术来移动它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文件和文件夹移动到指定目录? - How do I move both files and folders to the specified directory? 如何根据包含文件夹名称的文件将文件复制到文件夹中? - How do I copy files into folders based on the file containing the folder name? 如何根据文件夹名称将文件从一个带有子文件夹的目录复制到另一个带有子文件夹的目录? - How do I copy files from one directory with sub-folders to another directory with sub-folders based on folder name? 如何将文件移到相应的文件夹中 - how can I move files into corresponding folders Python:根据文件名的一部分将文件移动到文件夹 - Python: Move files to folder based on Part of File name python:我可以将基于名称一部分的文件移动到具有该名称的文件夹中 - python: can i move a file based on part of the name to a folder with that name 如何根据 python 中文件名的相似性将文件移动到文件夹 - How to move files to folders based on the similarity of filenames in python 如何根据Excel中的列表查找和移动某些文件? - How do I find and move certain files based on a list in excel? 如何使用python基于标签移动一定百分比的文件? - How do I move a percentage of files based on label with python? 如何根据文件名将一个文件夹中的多个文件分成不同的文件夹? - How can I separate many files from one folder into separate folders based on the name of the files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM