Copy directory contents into a directory with python [duplicate]

Solution 1:

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

Solution 2:

You can also use glob2 to recursively collect all paths (using ** subfolders wildcard) and then use shutil.copyfile, saving the paths

glob2 link: https://code.activestate.com/pypm/glob2/