如何在shutil.copytree中编写忽略的回调函数

发布于 2024-11-28 18:46:36 字数 732 浏览 5 评论 0原文

我对 python 比较陌生。我正在尝试将一个目录复制到另一个维护结构的目录。

我正在使用

    shutil.copytree(src, dst, symlinks=False, ignore=None, 
    copy_function=copy2, ignore_dangling_symlinks=False)

我正在尝试编写一个回调函数来忽略。

我的目标是获取列表中的文件列表,并仅复制这些文件,忽略其余文件。我们如何将列表传递到回调函数中?

我编写了一个简单的回调函数,但是当我尝试运行 copyTree 函数时出现一些错误

   def abc(src,names):
    print(src)
    print(names)



    Traceback (most recent call last):
   File "<pyshell#23>", line 1, in <module>
shutil.copytree('D:\Mytest','D:\PythonTestDest3',symlinks=False,ignore=abc)
  File "C:\Python32\lib\shutil.py", line 204, in copytree
if name in ignored_names:
  TypeError: argument of type 'NoneType' is not iterable

I am relatively new to python. I am trying to copy a directory to another directory maintaining the structure.

I am using

    shutil.copytree(src, dst, symlinks=False, ignore=None, 
    copy_function=copy2, ignore_dangling_symlinks=False)

I am trying to write a call back function for ignore.

My aim is to take a list of files in a list , and copy only those files,ignoring the rest. How do we pass a list into the call back function?

I wrote a simple call back function , but I get some error when I try to run the copyTree function

   def abc(src,names):
    print(src)
    print(names)



    Traceback (most recent call last):
   File "<pyshell#23>", line 1, in <module>
shutil.copytree('D:\Mytest','D:\PythonTestDest3',symlinks=False,ignore=abc)
  File "C:\Python32\lib\shutil.py", line 204, in copytree
if name in ignored_names:
  TypeError: argument of type 'NoneType' is not iterable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

錯遇了你 2024-12-05 18:46:37

如果给出了ignore,它必须是一个可调用的,它将接收正在访问的目录作为其参数......及其内容列表...可调用必须返回目录序列以及相对于当前目录的文件名...;这些名称将在复制过程中被忽略。

文档

我的目标是获取列表中的文件列表

尝试使用匿名函数 (lambda) 和列表理解:

copytree(src, dest,
         ignore=lambda d, files: [f for f in files
                                    if f not in files_to_copy
                                    and not is_dir(os.path.join(d, f))])

请注意,lambda 函数获取文件的基本名称并目录,而不是它们的完整路径;感谢@jwhitlock 的目录处理。

If ignore is given, it must be a callable that will receive as its arguments the directory being visited ... and a list of its contents ... The callable must return a sequence of directory and file names relative to the current directory ...; these names will then be ignored in the copy process.

(docs)

My aim is to take a list of files in a list

Try an anonymous function (lambda) and a list comprehension:

copytree(src, dest,
         ignore=lambda d, files: [f for f in files
                                    if f not in files_to_copy
                                    and not is_dir(os.path.join(d, f))])

Note that the lambda function gets the basenames of the files and directories, not their full paths; kudos @jwhitlock's for the directory handling.

静谧幽蓝 2024-12-05 18:46:36

忽略函数的返回需要是要忽略的目录和文件的列表。您没有返回任何内容,而是返回 None,因此您收到错误 TypeError: argument of type 'NoneType' is not iterable

下面是一个示例,它将复制文件夹结构和“copy_these”中列出的文件:

import os.path

copy_these = ['a.txt', 'b.txt', 'c.txt']

def ignore_most(folder, files):

    ignore_list = []
    for file in files:
       full_path = os.path.join(folder, file)
       if not os.path.isdir(full_path):
           if file not in copy_these:
               ignore_list.append(file)
    return ignore_list

The return of the ignore function needs to be a list of directories and files to ignore. You aren't returning anything, which returns None, so you are getting the error TypeError: argument of type 'NoneType' is not iterable.

Here's an example that will copy the folder structure and the files listed in 'copy_these':

import os.path

copy_these = ['a.txt', 'b.txt', 'c.txt']

def ignore_most(folder, files):

    ignore_list = []
    for file in files:
       full_path = os.path.join(folder, file)
       if not os.path.isdir(full_path):
           if file not in copy_these:
               ignore_list.append(file)
    return ignore_list
亣腦蒛氧 2024-12-05 18:46:36

shutil 模块提供了 ignore_patterns 函数。

此工厂函数创建一个可用作 copytree() 的忽略参数的可调用函数,忽略与所提供的 glob 样式模式之一匹配的文件和目录。

模块页面还显示了一些示例

The shutil module provides a ignore_patterns function.

This factory function creates a function that can be used as a callable for copytree()‘s ignore argument, ignoring files and directories that match one of the glob-style patterns provided.

The module page shows a couple of examples as well.

陌上芳菲 2024-12-05 18:46:36

忽略回调函数应返回与不应复制的“src”目录相关的名称列表。

您的示例回调不返回任何内容(即无)。然后 copytree 需要一个列表,尝试迭代它。因为它不能,所以你会得到这个例外。

The ignore callback function should return a list of names relative to the 'src' directory that should not be copied.

Your example callback returns nothing (ie. None). Then copytree, expecting a list, tries to iterate over it. Since it can't, you get that exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文