Search code examples
pythonshutilcopytree

Copy directory contents into a directory with python


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".


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: