迭代(或递归?)过滤目录并将结果复制到新目录 [英] Iteratively (or recursively?) filter directory and copy results to new directory

查看:184
本文介绍了迭代(或递归?)过滤目录并将结果复制到新目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很习惯Python,并且一直在教书本和StackOverflow。我似乎从这个网站的真实世界的例子中学到最好的。我希望有人可以帮我解决当前的问题。



我有一个约有1,000个文件的目录(文件夹)。这些文件具有前缀(模式)的名称,例如 aaa _ * bbb _ * ccc _ * 等等。我想遍历目录并复制所有具有指定前缀的文件,并将它们移动到具有该前缀名称的新目录。例如,找到 / my_dir_path 中的所有 aaa _ * 文件,并将其复制到 / my_new_dir_path / aaa ,同样所有的 bbb _ * / my_new_dir_path / bbb 。新目录已经创建。



以下简单的样板脚本用于将其中一个前缀模式复制到新目录:

  dir ='/ my_dir_path'
pattern ='aaa_ *'
dest ='/ my_new_dir_path / aaa'

for root,dirs,os.walk(dir)中的文件:
在fnmatch.filter(文件,模式)中的文件名:
source =(os.path.join(root,filename) )
shutil.copy2(source,dest)

我正在努力的是如何为我的模式列表递归运行 fnmatch.filter ,然后最终将文件输出到自己的目录。



我已经尝试使用 * args 的模式列表编写了许多不同的函数定义,但通常得到一个错误消息filter()只需要2个参数(3给定)。 (我很抱歉没有代码示例来显示这里,过去几天我已经有很多版本,我没有任何一个代表性的功能定义。)


$ b $我是一个这样的新手,我不知道如果 fnmatch.filter 甚至可以被递归,或者如何设计一个替代策略。然后,存在如何将每个模式搜索中生成的文件存储到其自己的目录中的问题。我想我可以提前创建一个字典,例如

  my_dict = {'my_new_dir_path / aaa':'aaa_ * ','my_new_dir_path / bbb':'bbb_ *'} 

并将生成的文件存储为适当的键,但是鉴于我甚至没有得到递归工作(即打印),我没有在如何将结果复制到一个新的目录。

解决方案

现在我没有测试过,因为我没有所有的文件要测试。但是,我只需要将pattern变量作为一个列表,并添加一个for循环。我也改变了dest变量,所以我们可以使用模式列表。

  dir ='/ my_dir_path'
patterns = ['aaa','bbb','ccc']
dest ='/ my_new_dir_path /'

为root,dirs,os.walk(dir)中的文件:
模式中的模式:
在fnmatch.filter(文件,模式+'_ *')中的文件名:
source =(os.path.join(root,filename))
shutil.copy2(source,dest + pattern +'/')


I’m new to Python and have been teaching myself with textbooks and StackOverflow. I seem to learn best from real world examples from this site. I’m hoping someone can help me with my current problem.

I have a directory (folder) with about 1,000 files. The files have names with prefixes (patterns) such as, aaa_*, bbb_*, ccc_* and so on. I would like to iterate through the directory and copy all the files that have the specified prefix and move them to a new directory with that prefix name. For example, find all the aaa_* files in /my_dir_path and copy them to /my_new_dir_path/aaa and likewise all the bbb_* to /my_new_dir_path/bbb. The new directories have already been created.

The following simple, boilerplate, script works for copying one of the prefix patterns to a new directory:

dir = '/my_dir_path'
pattern = 'aaa_*'
dest = '/my_new_dir_path/aaa'

for root, dirs, files in os.walk(dir):
    for filename in fnmatch.filter(files, pattern):
        source = (os.path.join(root, filename))
        shutil.copy2(source, dest)

What I’m struggling with is how to recursively run fnmatch.filter for my list of patterns and then, ultimately, output the files to their own directory.

I have tried writing a number of different function definitions with the list of patterns as *args, but would typically get an error message that "filter() takes exactly 2 arguments (3 given)". (I apologize for not having code examples to show you here. I’ve been through so many versions in the last few days that I don’t have any one representative function definition.)

I’m such a novice that I don’t know if fnmatch.filter can even be made recursive or how to devise an alternative strategy. Then, there is the problem of how to store the resulting files from each pattern search into its own directory. I was thinking that I could create a dictionary in advance, e.g.,

my_dict = {'my_new_dir_path/aaa': 'aaa_*', 'my_new_dir_path/bbb': 'bbb_*'}

and store the resulting files as values under the appropriate key, but given that I haven’t even gotten the recursion to work (i.e., print) I haven’t worked on how to copy the results to a new directory.

解决方案

Now I haven't tested this since I don't have all of the files to test with. However, I would just make the "pattern" variable a list and add another for loop. I also changed the "dest" variable so we can just utilize the "patterns" list.

dir = '/my_dir_path'
patterns = ['aaa', 'bbb', 'ccc']
dest = '/my_new_dir_path/'

for root, dirs, files in os.walk(dir):
    for pattern in patterns:
        for filename in fnmatch.filter(files, pattern+'_*'):
            source = (os.path.join(root, filename))
            shutil.copy2(source, dest+pattern+'/')

这篇关于迭代(或递归?)过滤目录并将结果复制到新目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆