There are a few ways you can rename and move files in Python. We are going to look at all of them.
Rename a file in a directory
Let’s create a text file called old_name.txt in C:\temp.
The easiest way to rename a file is to use the rename function from the os module.
Because rename is a part of the os module, you have to import it first. Next, use the rename function to change one name to another using absolute paths.
import os os.rename(r'C:\temp\old_name.txt', r'C:\temp\new_name.txt')
The letter “r” at the beginning of a path means that the backslashes inside the string should stay as they are.
We can also use double backslashes or one forward slash instead of using “r” at the beginning.
os.rename('C:\\temp\\old_name.txt', 'C:\\temp\\new_name.txt') os.rename('C:/temp/old_name.txt', 'C:/temp/new_name.txt')
Rename a file if exists
If we try to rename a file that doesn’t exist, we are going to get an error informing us that the system couldn’t find the file specified. That’s why it’s important to handle this.
We are going to use function os.path.exists to check whether there is such a file. If not, return information, otherwise, change the name.
import os from os import path original_path = r'C:\temp\non_existent_file.txt' new_path = r'C:\temp\new_name.txt' if path.exists(original_path): os.rename(original_path, new_path) else: print('The file does not exist')
So far, we were using absolute paths. This time, let’s set the current working directory to C:\temp and use the name of files.
import os os.chdir(r'C:\temp') os.rename('old_name.txt', 'new_name.txt')
Rename and move to a different directory
With the rename function, you can rename files but also move them. I’ll show you how to do it, using the following example.
In the C:\temp directory, create a new directory, called dir, and then run this code.
import os os.chdir(r'C:\temp') os.rename('old_name.txt', r'dir\new_name.txt')
It’s important for the dir folder to be present in the directory, otherwise, Python will return an error.
We can modify the code, so the program will create a directory if it’s not present.
import os os.chdir(r'C:\temp') old_file = 'old_name.txt' new_file = 'new_name.txt' directory = 'dir' if not os.path.exists(directory): os.makedirs(directory) os.rename(old_file, directory + '/' + new_file)
Move a file between two drives
So far, we were renaming and moving files to different locations, but always on the same drive (C). If you try to copy a file to a different drive (for example D), the system will return an error informing you that it can’t move the file to a different drive.
It doesn’t mean, you cannot do it. It just means that you have to use a different way.
We are going to use the move function from the shutil module that allows moving files between different drives. The code is going to look like this.
import shutil shutil.move(r'C:\temp\old_name.txt', r'D:\temp\new_name.txt')
The os.rename function doesn’t check whether the files are on different file systems, so it fails in such cases.
The shutil.move function works on a higher level than os.rename. Before moving a file, it checks whether the source and destination files reside on the same drive.
shutil.move copies the content of the source file and writes it to the destination file.
Difference between os.rename and os.renames
There is another function in the os module, called os.renames.
os.renames works recursively. It means that this function will create intermediate directories to assure that the new pathnames are good. In the end, it will remove directories using removedirs().
import os path = 'C:/temp' os.chdir(path) old_path = r'dir/old_name.txt' new_path = r'dir2/new_name.txt' os.renames(old_path, new_path)
The rename function results in an error, but the renames function will change the dir name and the file name without any problems.
Difference between os.rename and os.replace
There is another important function in the os module, called replace. The difference between this one and rename is that the rename function will result in an error if the destination path is already present, but replace will overwrite the file.
import os os.replace(r'C:\temp\old_name.txt', r'C:\temp\dir\new_name.txt')