[英]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.split
和os.path.join
重建文件所需的文件名,然后构造一个os调用以使用os-commands或此处采用的某种技术来移动它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.