In this tutorial, we’ll explore the different options you have at your disposal to move a file (or several files) from one location to another using simple Python code. We’ll explore two of the most used libraries for this operation: os and shutil.
Table of Contents
Use shutil.move() to move a file in Python
This library (maybe less known that os) offers several high-level file operations. To achieve our goal, we need to follow simple steps:
- Find the path of our file: if you use VS Code, you can use the “Copy Path” from the interactive menu of the file explorer. I advise to always use the Path and not the Relative Path to be sure that your script works, no matter where you put it. But it’s a matter of personal preference.
- Find the path of the new location: you cannot have a path for a file that does not exist, which is why you need to get the path of the folder you want to move the file to
- Execute the shutile.move() code using the required parameters
Let’s assume that we have the following folder structure: one from folder with two .txt files, one empty (for now) to folder and the move.py folder where our code would be located.
If we execute the following code:
file = '/Users/user/Documents/Python/move_files/from/file_1.txt'
directory = '/Users/user/Documents/Python/move_files/to'
shutil.move(file, directory)Code language: Python (python)
We’d end up moving the file_1.text file from the from to the to folder.
Under the hood, please note that shutil will copy your original file and when the copy is successful, it will remove it. If a file with the same name as the one you try to move already exists in the destination folder, shutil will throw an error:
Traceback (most recent call last):
File "/Users/user/Documents/Python/move_files/move.py", line 5, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/shutil.py", line 803, in move
raise Error("Destination path '%s' already exists" % real_dst)
shutil.Error: Destination path '/Users/user/Documents/Python/move_files/to/file_1.txt' already existsCode language: Python (python)
Use os.rename() to move a file
The os library is maybe the most known one when it comes to file manipulation. As we can expect, it can handle moving a file to another folder pretty well. That being said, the logic followed is different. Indeed, instead of copying your file as shutil does, os will rename the path of your file, which will automatically move it.
Key differences between both libraries:
- If a file with the same name already exists in your destination folder, os will throw a similar error to what we saw above with shutil
- As os does not create a copy as shutil does, if your file is protected, your file will not be moved.
- You need to specify old and new paths (as opposed to old path and new directory for shutil) in your code, as shown below
old_path = '/Users/antoine/Documents/Python/move_files/from/file_1.txt'
new_path = '/Users/antoine/Documents/Python/move_files/to/file_1.txt'
os.rename(old_path, new_path)Code language: Python (python)
Move several files in Python
In a real-life situation, it is very likely that you’d need to move more than one file. Let’s imagine, for instance, that you have to move all .txt files from our origin folder (from) to our destination folder (to). How can we achieve that?
We can leverage methods from the os library to do it with several lines of codes.
directory = '/Users/user/Documents/Python/move_files/from/'
new_directory = '/Users/user/Documents/Python/move_files/to/'
#list of files to move
for file in os.listdir(directory):
#build full path
full_path = directory + file
#move just .txt files
if '.txt' in file:
os.rename(directory+file, new_directory+file)Code language: Python (python)
Our code follows the following logic:
- List the files we have in our origin folder
- Create the full path (which is the concatenation of the folder and the file path)
- If the full path contains .txt, move it