I already know how to create and write a text file in Python, but how do I move that file to a different folder on my system?
Asked
Active
Viewed 110 times
-3
-
1https://stackoverflow.com/a/8858026/10306224 – Leemosh Jan 25 '21 at 17:58
1 Answers
0
You can either make a system call with os.system
:
import os
os.system("mv /path/to/file /path/to/destination")
or rename it:
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")
First solution works only in bash shells, second and third should be portable over all platforms. Third has the advantage that you can specify a folder as destination, the file will then be put into that folder with the same name as in the old location.

TheEagle
- 5,808
- 3
- 11
- 39