python 复制文件
时间: 2023-07-25 11:44:33 浏览: 48
你可以使用 Python 的 shutil 模块来复制文件。以下是一个简单的示例代码:
```python
import shutil
# 将源文件复制到目标文件
src_file = 'path/to/source/file'
dst_file = 'path/to/destination/file'
shutil.copy(src_file, dst_file)
# 或者将源文件夹复制到目标文件夹
src_dir = 'path/to/source/directory'
dst_dir = 'path/to/destination/directory'
shutil.copytree(src_dir, dst_dir)
```
请注意,如果目标文件/文件夹已经存在,它们将被覆盖。如果你想保留目标文件夹中的现有文件,你可以使用 shutil.copy2() 函数来复制文件,或者使用 os.path.exists() 函数来检查目标文件/文件夹是否存在。
相关问题
Python 复制文件
以下是两种Python复制文件的方法:
1. 使用shutil模块的copy()函数
```python
import shutil
# 源文件路径
src_file = 'path/to/source/file.txt'
# 目标文件路径
dst_file = 'path/to/destination/file.txt'
# 复制文件
shutil.copy(src_file, dst_file)
```
2. 使用os模块的system()函数执行系统命令
```python
import os
# 源文件路径
src_file = 'path/to/source/file.txt'
# 目标文件路径
dst_file = 'path/to/destination/file.txt'
# 执行系统命令复制文件
os.system(f'cp {src_file} {dst_file}')
```
Python复制文件
你可以使用Python的shutil模块来复制文件。以下是一个简单的示例代码:
```python
import shutil
# 源文件路径
src_file = '/path/to/source/file'
# 目标文件路径
dst_file = '/path/to/destination/file'
# 复制文件
shutil.copy(src_file, dst_file)
```
在上面的代码中,你需要将`/path/to/source/file`替换为你要复制的源文件的路径,将`/path/to/destination/file`替换为你要将文件复制到的目标路径。