Copy directory contents into a directory with python
copytree, python, shutil
Solution
I found this code working which is part of the standard library:
from distutils.dir_util import copy_tree
# copy subdirectory example
from_directory = "/a/b/c"
to_directory = "/x/y/z"
copy_tree(from_directory, to_directory)
Reference:
- Python 2: https://docs.python.org/2/distutils/apiref.html#distutils.dir_util.copy_tree
- Python 3: https://docs.python.org/3/distutils/apiref.html#distutils.dir_util.copy_tree
Problem
I have a directory /a/b/c that has files and subdirectories. I need to copy the /a/b/c/* in the /x/y/z directory. What python methods can I use? I tried `shutil.copytree("a/b/c", "/x/y/z")`, but python tries to create /x/y/z and raises an `error "Directory exists"`.