How to Copy a File to Another Location in Python?

Estimated read time 1 min read

To copy a file to another location in Python, you can use the shutil module. The shutil module provides functions for file operations, including file copying. Here’s an example of how you can copy a file to another location:

import shutil

# Specify the source file path
source_file = 'path/to/source_file.txt'

# Specify the destination directory
destination_directory = 'path/to/destination_directory/'

# Copy the file to the destination
shutil.copy(source_file, destination_directory)

In the code above, you need to provide the source_file path as the path of the file you want to copy. Then, specify the destination_directory as the directory where you want to copy the file to. The shutil.copy() function is used to perform the file copy operation.

After executing the code, the file specified by source_file will be copied to the location specified by destination_directory.

Make sure to replace 'path/to/source_file.txt' and 'path/to/destination_directory/' with the actual file path and destination directory you want to use for the file copy.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply