Renaming files in Python

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.

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.

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.

Relative paths

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.

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.

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.

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.

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().

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.