How to copy a directory and its contents to an existing location using Python? How to copy a directory and its contents to an existing location using Python? python python

How to copy a directory and its contents to an existing location using Python?


distutils.dir_util.copy_tree does what you want.

Copy an entire directory tree src to a new location dst. Both src and dst must be directory names. If src is not a directory, raise DistutilsFileError. If dst does not exist, it is created with mkpath(). The end result of the copy is that every file in src is copied to dst, and directories under src are recursively copied to dst. Return the list of files that were copied or might have been copied, using their output name. The return value is unaffected by update or dry_run: it is simply the list of all files under src, with the names changed to be under dst.

(more documentation at the above url)


Why not implement it on your own using os.walk?


For highlevel file operations like that use the shutil module and in your case the copytree function. I think that is cleaner than "abusing" distutils.

UPDATE:: Forget the answer, I overlooked that the OP did try shutil.