Python »How to rename a file in Python

We can rename files in Linux terminal using different approaches, one approach is using Python which is discussed in this article. We cannot write Python code directly in the terminal, we create a separate file in a text editor. “Rename” is one of the functions of the operating system, so we will use the OS module in Python for this purpose.

To rename a file using a Python OS module, follow the procedure below:

Rename file in Python using OS module in Ubuntu

Requirements:

  • The file must exist earlier
  • Any version of Python must 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 operating system operations that can be performed using the os.rename () function of the Python OS module.

Now follow the below procedure to change the filename using the Python OS module:

Create a file in Python: First you need to create a Python file using the nano editor, for example, we created a Python_file.py file using the command below:

$ nano python_file.py

You can set the filename of your choice.

Using the “os.rename” Function to Rename a File

To rename a file in the current directory: The following syntax for the “os.rename ()” function will be used to rename a file in the current directory:

$ os.rename("source file", "destination file")

  • source file: old filename
  • target file: new filename

Now write the following code in Python_file to rename “old_andreyex.txt” to “new_andreyex.txt” in the current directory:

“Import os” is used to import an OS module into a program:

import os

os.rename("old_andreyex.txt","new_andreyex.txt")

Press ctrl + s to save the file and ctrl + x to exit the file.

Now to execute the file, run the below command, I am using Python3 to execute Python_file.py as Python3 is preinstalled on Ubuntu system:

$ python3 python_file.py

And the ls command is used to list the directory files to check if the file to be renamed exists or not.

Now, to make sure the file is renamed or not, list the files of the current working directory again.

To rename a file in a directory separate from the current directory: To rename a file, the following syntax for the “os.rename ()” function will be used:

$ os.rename("source file", "destination file")

  • original file: file path + old file name
  • target file: file path + modified / new filename

Now write the following code in Python_file.py to rename “old_andreyex.txt” to “new_andreyex.txt” in your desktop directory:

import os

os.rename("/home/masterand/Desktop/old_andreyex.txt","/home/masterand/Desktop/new_lin

uxhint.txt")

Press ctrl + s to save the file and ctrl + x to exit the file.

Use the ls command to list the files in the desktop directory to check if the file to be renamed exists or not.

Now, to make sure the file is renamed or not, list the Desktop directory files again.

To rename multiple files: we can also change the name of multiple files at the same time. For example, I have three files in my desktop directory: file1, file2, file3, and we want to prefix “andreyex_” to all three filenames, and then run the following code in “Python_file.py”.

import os

for textfile in os.listdir("/home/masterand/Desktop"):

os.rename(textfile,f"/home/masterand/Desktop/andreyex_{textfile}")

You can change the team according to your requirements; you can choose the filenames and the directory where they exist as you choose.

Conclusion:

A Python OS module function called os.rename () is used to rename files using Python. We can rename files using different methods, but in this article, we discussed how to rename files in the current directory other than the current directory, and how to rename multiple files using the Python OS module. If you are a Python programmer and want to rename file / files using Python, this article will surely help you.

Related Posts