Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 961
user2434 Avatar asked Aug 10 '11 14:08

user2434


2 Answers

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
like image 65
jwhitlock Avatar answered Sep 30 '22 00:09

jwhitlock


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.

like image 39
miku Avatar answered Sep 29 '22 22:09

miku