File operations‎ > ‎File commands‎ > ‎

os.rename vs shutil.move

import os
import shutil

# moving files in python
shutil.move('file.txt', 'path/to/new/directory/')

While, in principle, both os.rename() and shutil.move() can be used to move files, shutil.moves() attempts to overcome limitations of os.rename() for the task of moving files.

Use shutil.moves() for moving files on disk!

a) arguments closer to mv command
requires to include the file name in both the source and destination arguments ('rename' !)
os.rename('path/to/file.txt', 'path/to/new/directory/file.txt')
while shutil.move() requires only the new directory as destination ('move' !)
shutil.move('path/to/file.txt', 'path/to/new/directory/')

b) support of different disks
If source and destination are on a different disk, shutil.move() first copies the source to destination and then removes the source.