The os module in Python offers a approach to work together with the working system. It presents a spread of features for performing duties comparable to studying, writing, creating, and deleting recordsdata. One of many features within the os module is known as os.rename(), which permits for renaming recordsdata and directories in Python.
This text covers the utilization of the os.rename() operate in Python, together with a number of examples demonstrating the right way to rename recordsdata.
Tips on how to Use os.rename() to Rename Information in Python?
In Python, the os.rename() operate is utilized to alter the names of each recordsdata and directories. The syntax for this operate is as follows:
os.rename(original_file, modified_file)
Within the given syntax, the primary parameter corresponds to the present title of the file or listing, whereas the second parameter represents the specified new title.
Now, let’s discover just a few examples to know the method of renaming recordsdata in Python.
Renaming a File in a Listing
Now we are going to rename a file that’s positioned inside the present working listing of the Python file. As each the Python file and the textual content file are in the identical listing, we don’t need to specify the whole path to entry the file. We simply entry the file utilizing its title and extension of the file.
Open the Python compiler and run the code:
import os
path1 = r“linuxhint.txt”
path2 = r“linuxhintTutorial.txt”
os.rename(path1, path2)
Right here this code will modify the title of the file which is positioned inside the present working listing of the Python file. The code makes use of the os.rename() operate, authentic title and modified file title are handed as its argument.
Right here we will see the file is renamed from linuxhint.txt to linuxhintTutorial.txt.
Renaming a File at a Particular Path
Now we are going to specify the trail of the textual content file as a result of it’s not positioned inside the present working listing of the Python file. Right here we are going to rename the file utilizing the os.rename() operate.
The under Python code will rename a textual content file testfile.txt to linuxhint.txt file.
import os
path1 = r“C:UsersKashif JavedDocumentstestfile.txt”
path2 = r“C:UsersKashif JavedDocumentslinuxhint.txt”
os.rename(path1, path2)
The above code makes use of the os.rename() operate, and we now have handed each file paths as its argument.
In output we will see the file title has modified.
Renaming A number of File Names in a Listing
We are able to additionally rename the a number of recordsdata that are positioned inside the identical listing by defining their full path. Subsequent, we are going to use a for loop to rename the recordsdata one after the other.
The under picture reveals three pattern recordsdata which will likely be now renamed.
Now within the code under we are going to specify the trail of those recordsdata and rename them.
import os
listing = r‘C:UsersKashif JavedDesktoppython’
extension = ‘.txt’
for filename in os.listdir(listing):
if filename.endswith(extension):
old_file = os.path.be a part of(listing, filename)
new_file = os.path.be a part of(listing, filename.change(extension, ‘_new.txt’))
os.rename(old_file, new_file)
Within the supplied code, the operate os.listdir() is used to acquire an inventory of all recordsdata inside a listing. Subsequent, a for loop is utilized to iterate by way of every file. An if assertion examines whether or not a file possesses a .txt extension. If this situation is met, the os.rename() operate will assign a brand new title to the file.
Right here we will see that every one recordsdata have been renamed efficiently.
Altering the Extension of a File
Utilizing the os.rename() operate, we will additionally rename the file extension. Now we are going to take the identical above file and alter the file’s extension from .txt to .csv file.
Following are the three pattern textual content recordsdata.
Now the under code will change the recordsdata extension:
import os
path_direct = r‘C:UsersKashif JavedDesktoppython’
for file in os.listdir(path_direct):
if file.endswith(‘.txt’):
new_filename = file.change(‘.txt’, ‘.csv’)
os.rename(os.path.be a part of(path_direct, file), os.path.be a part of(path_direct, new_filename))
Within the given code, the operate os.listdir() can learn all recordsdata in a folder. Then, utilizing a loop, every file within the record is checked. If a file has a .txt extension, it’s changed with a .csv extension utilizing the file.change() operate.
We are able to see that every one three file extensions have been modified efficiently from .txt to .csv file.
Conclusion
This text covers strategies of renaming recordsdata utilizing the os.rename() operate. It covers numerous situations, together with renaming recordsdata within the present working listing, renaming recordsdata at particular paths, renaming a number of recordsdata in a listing, and even altering file extensions. The examples supplied clarify completely different situations of renaming file names in Python utilizing the os.rename(). Learn extra about renaming recordsdata in Python on this article.