How can you copy a file in Python?
a) By using the shutil.copy() function
b) By using the os.copy() function
c) By using the file.copy() method
d) By using the os.rename() function
Answer:
a) By using the shutil.copy() function
Explanation:
To copy a file in Python, you can use the shutil.copy()
function from the shutil
module. This function takes the source file path and the destination file path as arguments, copying the file from the source to the destination.
import shutil
# Example of copying a file
shutil.copy("source.txt", "destination.txt")
In this example, the file source.txt
is copied to destination.txt
using the shutil.copy()
function.
Copying files is a common operation when backing up data, duplicating files for processing, or creating copies of templates.