How to write a call back function for ignore in shutil.copytree

不羁岁月 提交于 2019-12-05 03:32:18

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

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!