-3

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?

1 Answers1

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