When working with files in Python, you’ll often need to move files from one location to another. Python, with its rich library support and easy-to-understand syntax, is a fantastic language for automating file and directory manipulation tasks.
The Python standard library offers several modules you can use to move files and general file handling. The os, shutil, and pathlib modules, for example, provide ways to move files in Python. Robust scripts can incorporate exception handling to avoid system errors.
This article shows you how to move files using these three modules. You’ll also learn which technique is best for specific scenarios and how to incorporate error handling in your scripts.
As you follow the working examples throughout this article, you’ll become well-equipped to build effective scripts and projects to manage files using Python.
Let’s dive in!
How to Move Files With os.rename() in Python
The os library in Python provides a way of using functionality that’s dependent on the operating system it is running on. This includes the os.rename() function, which allows us to:
- Change a file or directory name.
- Move a file to a different place.
Although the name of the function suggests its sole use is to rename files, this section will show you how to use it to move a file to a new directory.
To start using the os module in your Python script, you first need to import it. The os.rename() takes two arguments:
- The current path and file name of the original file.
- The new path for the destination file.
The following code gives sample usage of moving a file from one directory to another:
import os source = "path/to/source/file.txt" destination = "path/to/destination/folder/file.txt" os.rename(source, destination)
2 Key Points of os.rename()
Here are two key points to be aware of:
- The function does not copy the file; it just updates the file’s location.
- The function replaces an existing file if it exists.
When to Use os.rename()
The os.rename() function is most suitable in situations where you simply need to move or rename file. It’s straightforward, easy to use, and doesn’t require any extra modules to be imported.
It is especially useful in scripting tasks for file management, such as:
- Periodically moving log files to an archive directory.
- Renaming a large number of files following a specific pattern.
That covers everything you need to know about the os.rename() function. In the next section, we’ll look at how you can move files using the shutil.move method.
How to Move a File With the shutil Module in Python
The shutil module in Python is used for high-level operations with files. The shutil.move method is used for moving files from one location to another. It also serves as a rename function.
This function takes two main arguments:
- The source file or directory path.
- The destination directory.
Here’s some sample code that moves a single file:
import shutil # Path to the file or directory old_file_path = "/home/user/old_directory/my_file.txt" # New path where to move the file or directory new_file_path = "/home/user/new_directory/my_file.txt" # Use shutil.move() to move the file shutil.move(old_file_path, new_file_path)
When to Use shutil.move()
The shutil.move() function is best used when you need more functionality than what os.rename() provides. Here are two use cases:
- You need to preserve file metadata.
- You want the destination folder to be created automatically if it doesn’t exist.
It’s particularly useful when writing scripts for managing large sets of files or directories, or when working with temporary files or directories.
Alternatively, you can combine shutil together with os to move multiple files. We go over how you can do that in the next section.
How to Combine “os” and “shutil” to Move Multiple Files
You can also move multiple files by combining the os module with the shutil.move() function. One way to do so is:
- Loop through all the files you want to move.
- Apply shutil.move() to each one.
Here’s an example of how to do it with a for loop:
import shutil, os # directory containing the files to move source_directory = "/home/user/old_directory/" # destination directory destination_directory = "/home/user/new_directory/" # get a list of all files in the directory files_to_move = os.listdir(source_directory) # loop through all files for file_name in files_to_move: source = source_directory + file_name destination = destination_directory + file_name # move each file shutil.move(source, destination)
In addition to shutil and os modules, you can also use the pathlib module to move files in Python. We discuss how to do that in the next section.
How to Use the pathlib Module to Move Files
The pathlib module in Python provides an object-oriented interface for dealing with file paths. It’s part of the Python standard library since version 3.4. If you’re not sure if you have the module, check out our article on how to check your version of Python.
Unlike os and shutil, pathlib doesn’t have a dedicated function for moving files. However, it does provide an easy and intuitive way to interact with the file system.
To move a file using pathlib, you can use the path.rename() function. This function works similarly to the os.rename() function.
Here is some sample code:
from pathlib import Path # Original path to the file old_file_path = Path("/home/user/old_directory/my_file.txt") # New path where to move the file new_file_path = Path("/home/user/new_directory/my_file.txt") # Use rename() to move the file old_file_path.rename(new_file_path)
We’ve covered a lot of ground and gone over the most popular ways to move around files in Python. In the next section, we’ll talk about how you can handle errors and exceptions when moving files in Python.
Handling Errors and Exceptions When Moving Files in Python
When using the three functions we’ve talked about in the previous sections to move a file in Python, you may encounter four common errors:
- FileNotFoundError: the source file does not exist.
- PermissionError: the process does not have sufficient permissions to perform the operation.
- IsADirectoryError: the function expects a file and gets a directory.
- NotADirectoryError: the function expects a directory and gets a file.
To handle these errors, you should use Python’s error handling mechanism, the try and except blocks.
Here is a detailed example with the os.rename() function. You can use similar code with the other functions you’ve learned in this article.
import os old_file_path = "/home/user/old_directory/my_file.txt" new_file_path = "/home/user/new_directory/my_file.txt" try: os.rename(old_file_path, new_file_path) except FileNotFoundError: print("The source file does not exist") except PermissionError: print("You do not have sufficient permissions to move the file") except IsADirectoryError: print("Expected a file, but got a directory") except NotADirectoryError: print("Expected a directory, but got a file")
In the above example, we use shutil.move() to move a file and handle different types of exceptions.
By incorporating error and exception handling in your Python code, you can create a more robust and user-friendly program that gracefully copes with unforeseen situations.
Working With Files in Python
You are often creating and moving files in order to load datasets using Python.
Check out this video for a walkthrough of several examples:
You’ve learned three different ways to move files in Python: the os module, the shutil module, and the pathlib module. Choosing which method to use depends largely on your specific requirements and the nature of the file management tasks you’re handling.
The os.rename() method is suitable for simple tasks, shutil.move() offers more flexibility and handles more complex situations, and pathlib provides an object-oriented way to deal with filesystem paths.
Whether you’re building a full-fledged file management application or writing a small utility script, or anything in between, knowing how to move files around is a valuable part of your Python skill set.