Copying files is a common task that programmers often have to deal with.
In Python, copying a file can be accomplished using the shutil module.
The shutil module provides a number of high-level operations on files and collections of files.
Here’s a Python program that demonstrates how to copy a file using the shutil module:
import shutil # specify the source file path source_file_path = '/path/to/source/file' # specify the destination file path destination_file_path = '/path/to/destination/file' # copy the file shutil.copy(source_file_path, destination_file_path
In this program, we first import the shutil module.
Then, we specify the path to the source file and the destination file.
Finally, we use the
shutil.copy() method to copy the source file to the destination file.
Note that if the destination file already exists,
shutil.copy() will overwrite it.
If you want to preserve the original file, you can use
That’s all there is to it!
With just a few lines of code, you can easily copy a file in Python.