copy a directory in python

You can use the shutil module to copy a directory in Python. The shutil module provides functions for file operations, and one of the functions is copytree(), which recursively copies an entire directory tree from one location to another.

Here's an example of how to use the copytree() function:

main.py
import shutil
import os

# specify the directory to copy
source_dir = '/path/to/source/directory'

# specify the destination directory
dest_dir = '/path/to/destination/directory'

# copy the directory recursively
shutil.copytree(source_dir, dest_dir)
251 chars
12 lines

In this example, we first specify the source directory and the destination directory. We then use the copytree() function to copy the source directory recursively to the destination directory. Note that the destination directory must not already exist, otherwise the function will raise an error. If you need to overwrite the destination directory, you can use the rmtree() function from the os module to delete the destination directory before calling copytree().

gistlibby LogSnag