How to use os.rename() method move Files in Python In Python
rename() method takes two arguments first one is the source path and the second one is the destination path, the rename function will move the file at the source path to the provided destination.
Code:
Python3
import os source = '/home/tuhingfg/Documents/source' destination = '/home/tuhingfg/Documents/destination' # gather all files allfiles = os.listdir(source) # iterate on all files to move them to destination folder for f in allfiles: src_path = os.path.join(source, f) dst_path = os.path.join(destination, f) os.rename(src_path, dst_path) |
How to move all files from one directory to another using Python ?
In this article, we will see how to move all files from one directory to another directory using Python. In our day-to-day computer usage we generally copy or move files from one folder to other, now let’s see how to move a file in Python:
This can be done in two ways:
- Using os module.
- Using shutil module.