Python Program to Get an Extension of File Using splittext()
# Python Program to Get an Extension of File Using splittext()
# importing os module
import os
# this will return a tuple of root and extension
split_tup = os.path.splitext('file.txt')
print(split_tup)
# extract the file name and extension
file_name = split_tup[0]
file_extension = split_tup[1]
print("File Name: ", file_name)
print("File Extension: ", file_extension)
Output:
('file', '.txt')
File Name: file
File Extension: .txt
Python Program to Get an Extension of File Using pathlib
# Python Program to Get an Extension of File Using pathlib
# importing pathlib module
import pathlib
# function to return the file extension
file_extension = pathlib.Path('test.txt').suffix
print("File Extension: ", file_extension)
Output:
File Extension: .txt
About The Author
Shailendra Bramhvanshi, Founder at Techaroha Solutions Private Limited and CEO at Newtum Solutions Private Limited. BlockChain Experts with 12 years of IT Experience in of all aspects of running Organization and Project Management. I have created different coins to help the business to bring more trust. Recently deployed a stable coin in India TrueINR and working on few cryptocurrency exchanges. My Team is being rated as a top 10 blockchain service provider in India by Silicon India