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'
>>> 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.