Python Program - Copy a File
here's a Python program that copies a file from one location to another:
refe:ot rtheitroad.com# import the shutil module import shutil # define the source and destination file paths src_file = "/path/to/source/file" dst_file = "/path/to/destination/file" # copy the file from source to destination shutil.copy(src_file, dst_file) # print a message to indicate success print("File copied successfully!")
In this program, we first import the shutil
module using the import
keyword.
We then define the source and destination file paths by replacing /path/to/source/file
and /path/to/destination/file
with the actual file paths. Note that both paths can be absolute or relative.
We then use the shutil.copy()
function to copy the file from the source path to the destination path.
Finally, we use the print()
function to print out a message indicating that the file was copied successfully. If an error occurs during the copy operation, a Python exception will be raised.