pythoncopytree

Python. Copy Files and Folders to existing directory


How can I copy all contents (including folders) from folder A to the existing folder B with Python?


Solution

  • You can use the copytree function from the shutil package https://docs.python.org/3/library/shutil.html#shutil.copytree.

    from shutil import copytree
    
    src_path = "path/of/your/source/dir"
    dest_path = "path/of/your/destination/dir"
    
    copytree(src_path, dest_path, dirs_exist_ok=True)
    

    The dirs_exist_ok=True parameter is useful if the dest_path already exists, and you want to overwrite the existing files.