Change File Extension In Python
Last Updated :
01 Feb, 2024
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.
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.
Python3
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.
Python3
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.
Python3
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.
Similar Reads
How to Change File Extension in Mac? File Extensions or Filename Extensions are algorithm-specific elements that can be opened only with the specific application based on the algorithm mentioned there. There are a large number of file extensions present and can be different based on the Operating System. Like, the compressed file forma
3 min read
How to Change File Extension in Windows? On Windows, there are different applications present to open each kind of file. For example, the Word Application can only open DOC files, and the PDF Viewer can only able to open PDF files, not DOC or PNG files. Applications mark any file as openable or not using one kind of pointer which is known
6 min read
How to get file extension in Python? In this article, we will cover How to extract file extensions using Python. How to Get File Extension in Python? Get File Extension in Python we can use either of the two different approaches discussed below: Use the os.path Module to Extract Extension From File in PythonUse the pathlib Module to Ex
2 min read
Python | Passing Filenames to Extension in C Filename has to be encoded according to the systemâs expected filename encoding before passing filenames to C library functions. Code #1 : To write an extension function that receives a filename CPP static PyObject* py_get_filename(PyObject* self, PyObject* args) { PyObject* bytes; char* filename; P
2 min read
Python - Change List Item Lists in Python are mutable meaning their items can be changed after the list is created. Modifying elements in a list is a common task, whether we're replacing an item at a specific index, updating multiple items at once, or using conditions to modify certain elements. This article explores the dif
3 min read