Difference Between os.rename and shutil.move in Python
Last Updated :
28 Apr, 2025
When it comes to renaming or moving files and directories, two commonly used methods are os. rename and shutil. move. While both can serve the purpose of renaming and moving, they have distinct differences in terms of functionality and use cases. In this article, we will explore these differences and when to use each of them.
Difference Between os. rename and shutil. move in Python
Both os. rename and shutil. move are valuable tool in Python for renaming and moving files and directories. The choice between them depends on the complexity of your task and the specific requirements of your file manipulation operation.
When to Use os. rename and shutil.move:
Use of os.rename when:
- You need to perform a basic rename operation within the same directory.
- Atomicity is crucial for your operation.
Use of shutil.move when:
- You need to move files or directories across different filesystems or devices.
- You want to ensure destination directories are automatically created.
- You need to handle the overwriting of existing files.
- You want to preserve file metadata and attributes.
Examples for os.rename
Renaming a File
It will rename the file 'old_file.txt' to 'new_file.txt' and display a message indicating that the renaming process was successful.
Python3
import os
# Current file name
old_name = 'old_file.txt'
# New file name
new_name = 'new_file.txt'
# Rename the file
os.rename(old_name, new_name)
print(f"File '{old_name}' has been renamed to '{new_name}'.")
Output:
File is renamed as GFGRename a Directory
It will rename the directory 'old_directory' to 'new_directory' and display a message indicating that the renaming process was successful.
Python3
import os
# Current directory name
old_dir = 'old_directory'
# New directory name
new_dir = 'new_directory'
# Rename the directory
os.rename(old_dir, new_dir)
print(f"Directory '{old_dir}' has been renamed to '{new_dir}'.")
Output:
Rename a DirectoryExamples for shutil.move
Moving a File to a Different Directory
This code uses the shutil module to move a file from a source location to a destination directory. It first specifies the paths to the source file and the destination directory. Then, it calls shutil.move() with these paths to perform the move operation.
Python3
import shutil
# Source file path
source_file = 'source_directory/source_file.txt'
# Destination directory path
destination_directory = 'destination_directory/'
# Move the file to the destination directory
shutil.move(source_file, destination_directory)
print(f"File '{source_file}' has been moved to '{destination_directory}'.")
Output:
Moving a file to different directory Handling Overwriting with shutil.move
This code uses the shutil library in Python to move a file from a source directory to a destination directory, potentially overwriting a file with the same name in the destination directory.
Python3
import shutil
# Source file path
source_file = 'source_directory/file.txt'
# Destination directory path
destination_directory = 'destination_directory/'
# Destination file path (same name as the source)
destination_file = 'destination_directory/file.txt'
# Move the file to the destination directory (overwrites if exists)
shutil.move(source_file, destination_file)
print(f"File '{source_file}' has been moved to '{destination_file
}' (overwriting if necessary).")
Output:
Handling Overwriting with shutil.move
Similar Reads
Difference between casefold() and lower() in Python In this article, we will learn the differences between casefold() and lower() in Python. The string lower() is an in-built method in Python language. It converts all uppercase letters in a string into lowercase and then returns the string. Whereas casefold() method is an advanced version of lower()
2 min read
Delete an entire directory tree using Python | shutil.rmtree() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps in automating the process of copying and removal of files and directories. shutil.rmtree() is used to delete an entire direc
3 min read
Difference between modes a, a+, w, w+, and r+ in built-in open function? Understanding the file modes in Python's open() function is essential for working with files effectively. Depending on your needs, you can choose between 'a', 'a+', 'w', 'w+', and 'r+' modes to read, write, or append data to files while handling files. In this article, we'll explore these modes and
4 min read
Python | os.getresuid() and os.setresuid() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. All functions in os module raise OSError in the case of invalid or inaccessible f
4 min read
Python | os.geteuid() and seteuid() method All functions in the OS Module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. os.geteuid() method in Python is used to get the current processâs effective user ID while os.seteuid
3 min read