26-Jan-2022
.
Admin
Hi Guys,
In this blog, I will learn you how to get file extension in python. we will talk about python get file extension. You can extract the file extension of a filename string using python.
This article will give you example of how to get extention of file using python. Python has a module os.path that has pre-made useful utility functions to manipulate OS file paths. It includes opening, saving and updating, and getting the information from file paths.
In Python, we can extract the file extension using either of the two different example as bellow:
Example 1 :
# import os module
import os
# return filename and extension
fileInfo = os.path.splitext('demo.txt')
print(fileInfo)
# extract extension
extension = fileInfo[1]
print("File Extension: ", extension)
Output
File Extension: txt
Example 2 :
# import pathlib module
import pathlib
# return the file extension
extension = pathlib.Path('demo.html').suffix
print("File Extension: ", extension)
Output
File Extension: html
It will help you....
#Python