【发布时间】:2021-04-29 13:32:18
【问题描述】:
我已经知道如何在 Python 中创建和编写文本文件,但是如何将该文件移动到系统上的其他文件夹中?
【问题讨论】:
标签: python python-3.x
我已经知道如何在 Python 中创建和编写文本文件,但是如何将该文件移动到系统上的其他文件夹中?
【问题讨论】:
标签: python python-3.x
您可以使用os.system 进行系统调用:
import os
os.system("mv /path/to/file /path/to/destination")
或重命名:
os.rename("/path/to/file", "/path/to/destination")
or move it with `shutil.move`:
```python
import shutil
shutil.move("/path/to/file", "/path/to/destination")
第一个解决方案仅适用于 bash shell,第二个和第三个应该可以在所有平台上移植。第三个优点是您可以指定一个文件夹作为目标,然后该文件将被放入与旧位置同名的文件夹中。
【讨论】: