To rename file using Python OS module follow the procedure given below:
Rename file in Python Using OS module on Ubuntu
Requirements:
- File should exist previously
- Any Python version should be installed on your Linux System
The Python OS module allows you to perform various operations related to the Operating System. Renaming a file is also one of the operations of the Operating System which can be performed using Python OS module function os.rename().
Now follow the procedure below to change the name of file using Python OS module:
Creating Python File : First you need to create a Python file using nano, for instance I am creating Python_file.py file by below mentioned command:
You can set the filename of your choice.
Using “os.rename” Function to Rename File
To rename file in Current directory: Below mentioned “os.rename()” function syntax will be used to rename file in current directory:
- source file: old filename
- destination file: new filename
Now write the below mentioned code into the Python_file to rename the “old_linuxhint.txt” to “new_linuxhint.txt” in current directory:
The “import os” is used to import the OS module in the program.
os.rename("old_linuxhint.txt","new_linuxhint.txt")
Press “ctrl+s” to save the file and “ctrl+x” to exit the file.
Now to execute the file run the below mentioned command, I am using Python3 to execute Python_file.py as Python3 is pre installed in Ubuntu system:
And ls command is used to list the files of directory to check whether the file to be renamed exists or not:
Now to verify that file is renamed or not, again list the files of current working directory:
To rename file in Directory apart from Current Directory: Below mentioned “os.rename()” function syntax will be used to rename file:
- source file: path of the file + old filename
- destination file: path to file + modified/new filename
Now write the below mentioned code into the Python_file.py to rename the “old_linuxhint.txt” to “new_linuxhint.txt” on Desktop directory:
os.rename("/home/alishba/Desktop/old_linuxhint.txt","/home/alishba/Desktop/new_lin
uxhint.txt")
Press “ctrl+s” to save the file and “ctrl+x” to exit the file.
Use ls command to list the files of Desktop directory to check whether the file to be renamed exists or not:
Now to verify that file is renamed or not, again list the files of Desktop directory:
To rename multiple files: We can also change the name of multiple files at a time. For instance, I have three files in my Desktop directory as file1, file2, file3 and I want to add the prefix of “linuxhint_” to all of the three filenames, then run the below mentioned code in “Python_file.py”.
for textfile in os.listdir("/home/alishba/Desktop"):
os.rename(textfile,f"/home/alishba/Desktop/linuxhint_{textfile}")
You can modify the command according to your requirements; you can choose the filenames and directory in which they exist of your choice:
We will use the “listdir()” function in “for loop” to get files one by one and then rename them using the os.rename function. The f string function of Python in above given code is used to change the name of each file in the desktop to “/home/alishba/linuxhint_{old filename}”. Here we are using “textfile” as loop variable for old filename. which will get each file of directory. Press “Ctrl+s” to save the file and “Ctrl+x” to exit the file.
Firstly, list the files of Desktop to check existence of files and then execute the file to rename files:
Now again list the files of Desktop to verify that files are renamed properly:
In the above picture we can clearly see that all files of the Desktop directory are renamed.
Conclusion:
Python OS module function called os.rename() is used to rename files using Python. We can rename files using different techniques but in this article we discussed how to rename files in the current directory, apart from the current directory and to rename multiple files using the Python OS module. If you are a Python programmer and want to rename file/files using Python then this article will surely help you.