![]() |
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 How To Change File Extension In Python?Below, are the ways To Change File Extension In Python.
Change File Extension Using
|
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!
shutil
ModuleThe 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!
pathlib
ModuleThe 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!
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.
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |