Python - How to move file to another directory

By xngo on June 16, 2019

Python provides multiple ways to move a file to another directory. You can use the following methods:

import os
import shutil
 
os.rename("path/to/file.docx", "new/destination/file.docx")
shutil.move("path/to/file.docx", "new/destination/file.docx")

Note: You must include the filename(file.docx) in both the source and destination arguments. If the filename is changed, then the file will be renamed as well as moved.

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.