Python 3 – os.rename() Method
When working with files in Python, you may sometimes need to rename files or directories. The os module in Python provides a method called rename()
that allows you to rename files or directories easily.
Syntax
The syntax for the os.rename()
method is as follows:
os.rename(src, dst)
Here, src
represents the source file or directory name, and dst
represents the destination file or directory name. The method renames the file or directory at src
to dst
.
It is important to note that os.rename()
will raise an error if the source file or directory does not exist or if the destination file or directory already exists.
Example 1: Renaming a File
In this example, we will rename a file using the os.rename()
method. The code below renames a file called oldfile.txt
to newfile.txt
:
import os
os.rename("oldfile.txt", "newfile.txt")
Here, oldfile.txt
is the source file name, and newfile.txt
is the destination file name. The code will rename oldfile.txt
to newfile.txt
.
Example 2: Renaming a Directory
In this example, we will rename a directory using the os.rename()
method. The code below renames a directory called olddir
to newdir
:
import os
os.rename("olddir", "newdir")
Here, olddir
is the source directory name, and newdir
is the destination directory name. The code will rename olddir
to newdir
.
Example 3: Handling Errors
As mentioned earlier, the os.rename()
method will raise an error if the source file or directory does not exist or if the destination file or directory already exists. In this example, we will handle these errors using the try-except block:
import os
try:
os.rename("oldfile.txt", "newfile.txt")
except FileNotFoundError:
print("Source file does not exist.")
except FileExistsError:
print("Destination file already exists.")
Here, we have used the try-except block to catch any errors that might occur. If the source file does not exist, the code will print “Source file does not exist.” If the destination file already exists, the code will print “Destination file already exists.”
Example 4: Changing File Extensions
In this example, we will change the extension of a file using the os.rename()
method. The code below changes the extension of a file called oldfile.txt
to .html
:
import os
os.rename("oldfile.txt", "newfile.html")
Here, oldfile.txt
is the source file name, and newfile.html
is the destination file name. The code will change the extension of oldfile.txt
to .html
.
Example 5: Renaming Multiple Files
In this example, we will rename multiple files using the os.rename()
method. The code below renames all files in the current directory that have the extension .txt
:
import os
for filename in os.listdir("."):
if filename.endswith(".txt"):
os.rename(filename, filename.replace(".txt", ".html"))
Here, we have used a for loop to iterate through all files in the current directory. If a file has the extension .txt
, the method os.rename()
is used to change the extension to .html
.
Conclusion
The os.rename()
method in Python is a powerful tool when working with files and directories. With just a few lines of code, you can easily rename files or directories, change file extensions, and even rename multiple files at once. However, it is important to handle errors appropriately in order to prevent unexpected behavior. With this method, you can easily manipulate your files and directories in Python.