Home >Backend Development >Python Tutorial >How Do I Copy Files in Python and Preserve Metadata?
To copy a file in Python, the shutil module provides several methods. One commonly used method is shutil.copyfile():
import shutil shutil.copyfile('src_file_path', 'dest_file_path')
Here, you specify the source file path as src_file_path and the destination file path as dest_file_path.
Note:
If you need to preserve file metadata like time stamps, use the shutil.copy2() method:
shutil.copy2('src_file_path', 'dest_file_path')
If you prefer to use the os.path module, use the copy function instead of copyfile. However, keep in mind that copy accepts path names as str while copyfile only accepts strings.
The above is the detailed content of How Do I Copy Files in Python and Preserve Metadata?. For more information, please follow other related articles on the PHP Chinese website!