Last modified: Mar 19, 2025 By Alexander Williams

Python Pathlib replace() Explained

Python's pathlib module simplifies file and directory operations. One of its useful methods is replace(). This method helps rename or move files and directories.

In this article, we'll explore how to use replace() effectively. We'll also provide examples to make it easy to understand.

What is pathlib.replace()?

The replace() method is part of the pathlib.Path class. It replaces the contents of a file or directory with another. This is useful for renaming or moving files.

Unlike rename(), replace() works even if the target file or directory already exists. It will overwrite the target without raising an error.

How to Use pathlib.replace()

To use replace(), you need to create a Path object. Then, call the replace() method with the target path as an argument.

Here's a simple example:


from pathlib import Path

# Create a Path object for the source file
source = Path("old_file.txt")

# Replace the source file with a new file
source.replace("new_file.txt")
    

In this example, old_file.txt is renamed to new_file.txt. If new_file.txt already exists, it will be overwritten.

Example: Moving a File

You can also use replace() to move a file to a different directory. Here's how:


from pathlib import Path

# Create a Path object for the source file
source = Path("documents/old_file.txt")

# Move the file to a new directory
source.replace("backup/new_file.txt")
    

This moves old_file.txt from the documents directory to the backup directory. The file is also renamed to new_file.txt.

Example: Replacing a Directory

replace() can also be used with directories. Here's an example:


from pathlib import Path

# Create a Path object for the source directory
source = Path("old_directory")

# Replace the source directory with a new one
source.replace("new_directory")
    

This renames old_directory to new_directory. If new_directory already exists, it will be overwritten.

Key Points to Remember

Overwriting: replace() will overwrite the target file or directory if it exists. Be cautious when using it.

Cross-Device Operations: replace() works across different devices, unlike some other methods.

Atomicity: The operation is atomic, meaning it either completes fully or not at all.

Comparison with rename()

replace() is similar to rename(), but with a key difference. rename() raises an error if the target exists, while replace() overwrites it.

For more details on rename(), check out our guide on Python Pathlib rename() Explained.

Handling Errors

While replace() is powerful, it can still raise errors. For example, if the source file doesn't exist, a FileNotFoundError will be raised.

Always ensure the source file or directory exists before calling replace(). You can use Check File Existence with Python Pathlib.exists() to verify.

Conclusion

The pathlib.replace() method is a versatile tool for renaming and moving files and directories. It simplifies file management tasks in Python.

By understanding how to use replace(), you can handle file operations more efficiently. For more tips, explore our guides on Master Python Pathlib Joinpath for File Paths and other pathlib methods.