I am trying to copy a directory tree using shutil
in Python.
I am doing it like this:
shutil.copytree(source,target,False,lambda x,y:[r for r in y if os.path.isfile(r)]);
where source
is the path to the source directory, and target
is the name for a non-existent directory inside which the copy of source
is going to occur.
The third argument indicates the treatment of symbolic links.
The last argument, from what I understood in the documentation should be a function that inputs two parameters and returns a list of file-names that will be excluded from the copy. The first input is the name of the current directory, as shutil
travels the tree recursively, and the second the list of its contents.
This is why I input a lambda trying to return those elements in the list that are files.
But this is not working. It is copying everything.
Where am I getting confused?
What I am trying to do is, if I have
source\
subdir1\
file11.txt
file12.txt
subdir2\
file21.txt
I want to obtain
target\
subdir1\
subdir2\
By the way, I guess I would be able to write the copy myself using walk
or glob
, but I thought shutil
would be straightforward to use.