shutil.copy
时间: 2023-11-05 19:04:37 浏览: 132
Python shutil拷贝文件模块.docx
shutil.copy is a Python function that is used to copy a file from one location to another. The function takes two arguments: the source file, which is the file that you want to copy, and the destination file, which is the file that you want to copy the source file to.
Here's an example:
```python
import shutil
source_file = "C:/Users/Username/Documents/file.txt"
destination_file = "C:/Users/Username/Desktop/file.txt"
shutil.copy(source_file, destination_file)
```
In this example, we are copying the file "file.txt" from the "Documents" folder to the "Desktop" folder. The shutil.copy function will create a new copy of the file in the destination folder with the same name.
If the destination file already exists, shutil.copy will overwrite it without warning. However, if you want to preserve the original file's metadata, you can use shutil.copy2 instead.
Note that shutil.copy will only copy the file itself, not any of the file's metadata or permissions. If you want to copy the file along with its metadata and permissions, you can use shutil.copy2 or shutil.copyfile.
阅读全文