How to Rename a File in Python

Python can be used to perform actions on a file system, for example you can rename a file in Python, copy a file or delete a file and so on. This article will show you how you can rename files using Python.

Linux has a bunch of built in tools for renaming directories and files, but sometimes you may want to do this as part of a Python program. For example, you may want to take a copy of a group of files as a backup, then append -bak to their name.

Rename File and Directories using Python os.rename()

In Python, the rename() method is used to rename a file or directory. This method is a part of the os module . The syntax for using os.rename is as follows:

os.rename('source', 'destination')

Simple as that! The source is the full path to the source file name, and the destination is the full path to the target / desired file name. So, if we wanted to rename /home/user/file1 to /home/user/file2 we could use the following Python code:

This would work the same if we wanted to rename the file extension. For example:

import os
os.rename('/home/cloud_user/file1.txt','/home/cloud_user/file1.bak')

This will replace the .txt extension on file1 and rename it to .bak. The same approach will work to rename a directory using Python:

import os
os.rename('/home/cloud_user/directory1','/home/cloud_user/directory2')

If there are any files contained within the directory being renamed, they will be unaffected.

Rename Multiple Files using Python

What if we had a bunch of files called file1, file2 and file3 all in a directory, and we wanted to append the .bak file extension to each? We could rename them all, by creating a FOR loop in Python. For example:

import os
os.chdir('/home/cloud_user/dir3/')
for file in os.listdir(os.getcwd()):
    src=file
    dst=file+".bak"
    os.rename(src,dst)

Lets break this down. First of all os.chdir is used to set the working directory. This is the location where the files to be renamed are located. Next os.listdir is used, with os.getcwd to list the contents of the directory. Finally, the for loop iterates through each file it finds, and appends .bak to each one. This is the resultant file listing:

drwxrwxr-x  2 cloud_user cloud_user 4096 Dec 12 13:57 ./
drwxr-xr-x 22 cloud_user cloud_user 4096 Dec 12 14:13 ../
-rw-rw-r--  1 cloud_user cloud_user    0 Dec 12 13:30 file1.bak
-rw-rw-r--  1 cloud_user cloud_user    0 Dec 12 13:45 file2.bak
-rw-rw-r--  1 cloud_user cloud_user    0 Dec 12 13:46 file3.bak

If we wanted to rename the files entirely, for example if we had a bunch of random filenames in a directory – a typical example would be image files, we could iterate though the files and rename them, photo1.jpg, photo2.jpg and so on using similar code to the example above, but with a couple of changes:

import os
os.chdir('/home/cloud_user/dir3/')
i = 1
for file in os.listdir(os.getcwd()):
    src=file
    dst="photo"+str(i)+".jpg"
    os.rename(src,dst)
    i+=1

In this example we are changing the destination string to rename the file using photo plus a number, which is incremented on each loop. The resultant file listing is:

drwxrwxr-x  2 cloud_user cloud_user 4096 Dec 12 14:20 ./
drwxr-xr-x 22 cloud_user cloud_user 4096 Dec 12 14:19 ../
-rw-rw-r--  1 cloud_user cloud_user    0 Dec 12 14:20 photo1.jpg
-rw-rw-r--  1 cloud_user cloud_user    0 Dec 12 14:20 photo2.jpg
-rw-rw-r--  1 cloud_user cloud_user    0 Dec 12 14:20 photo3.jpg
-rw-rw-r--  1 cloud_user cloud_user    0 Dec 12 14:20 photo4.jpg

Use caution with any scripts that will modify multiple files. There’s no safety net with the examples given here, so make sure that you fully test any code you are planning to use.

Final Thoughts

This article has shown how to rename a file in Python and some techniques for how to rename multiple files using python.

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More