Home » Python

Extracting extension from filename in Python

Here, we are going to learn how to extract the extension from filename in Python programming language?
Submitted by Sapna Deraje Radhakrishna, on November 28, 2019

Python provides built in module to extract the file extension from the file name.

Before python3

The os.path module provides a function called splitext method, which splits the pathname path into file name and file extension.

Example:

>>> import os
>>> file_name = 'includehelp.txt'
>>> print(os.path.splitext(file_name))
('includehelp', '.txt')
>>> 

After python3

In the path from pathlib, using the suffix method.

Example:

>>> from pathlib import Path
>>> Path(file_name).suffix
'.txt'
>>> 


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.