Change File Extension In Python
Changing file extensions in Python can be a common task when working with files. Whether you need to modify file types for compatibility, organize your files, or perform some other operation, Python provides several methods to achieve this. In this article, we will explore four different methods to change file extensions using Python.
What is File Extension?
A File Extension is a suffix appended to the end of a filename to indicate the type of file and the format it is in. It is usually separated from the filename by a dot (period) and typically consists of three or four characters. File extensions are commonly used in operating systems to associate files with specific applications or to recognize the file format.
Example :
.jpg - JPEG image file
.docx - Microsoft Word document file
pdf - Adobe PDF document
.py - Python source code file4
How To Change File Extension In Python?
Below, are the ways To Change File Extension In Python.
- Using
OS
Module - Using
shutil
Module - Using
pathlib
Module
Change File Extension Using os
Module
Python OS
module in Python provides a simple and platform-independent way to interact with the operating system. We can leverage this module to rename files and change their extensions. In this example, below method extracts the base name of the file using os.path.splitext
and then creates a new file path by appending the desired extension. The os.rename
function is then used to rename the file.
import os
def change_extension(file_path, new_extension):
base_name, _ = os.path.splitext(file_path)
new_file_path = base_name + "." + new_extension
os.rename(file_path, new_file_path)
# Example usage:
change_extension("example.txt", "csv")
print("Successfully Changed!")
Output :
Successfully Changed!
Change File Extension Using shutil
Module
The shutil
module provides a higher-level interface for file operations, including file renaming. This module simplifies the process of renaming files compared to using the os
module directly. Here, we use shutil.move
to achieve the same result as in the first method. This method is often considered more readable and may be preferred for its simplicity.
import shutil
def change_extension(file_path, new_extension):
base_name, _ = os.path.splitext(file_path)
new_file_path = base_name + "." + new_extension
shutil.move(file_path, new_file_path)
# Example usage:
change_extension("example.txt", "csv")
print("Successfully Changed!")
Output :
Successfully Changed!
Change File Extension Using pathlib
Module
The pathlib
module provides an object-oriented approach to file system paths, making it more convenient to manipulate paths and filenames. In this method, we use the pathlib.Path
class to represent the file path. The with_suffix
method is then used to change the file extension, and rename
is used for the actual file renaming.
from pathlib import Path
def change_extension(file_path, new_extension):
path = Path(file_path)
new_file_path = path.with_suffix("." + new_extension)
path.rename(new_file_path)
# Example usage:
change_extension("example.txt", "csv")
Output :
Successfully Changed!
Conclusion
Changing file extensions in Python can be accomplished through various methods, each offering its own advantages. Whether you choose the straightforward approach of the os
module, the convenience of shutil
, the object-oriented elegance of pathlib
, or the legacy support of os.path
, the key is to understand the requirements of your project and select the method that best suits your needs.