Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
3 views
in Python by (2.6k points)
edited by
I want to extract an extension from a filename in Python, How can I do that?

2 Answers

0 votes
by (46k points)
edited by

There are many ways to do it, I'll discuss a few with you:

  • Use pathlib

import pathlib
 

print(pathlib.Path('xyz path').suffix) 

Or

  •  Use import function

import os.path 

extension = os.path.splitext(xyz file)[2][2:] 

Happy Learning......!! 

Learn more about Python from an expert. Enroll in our Python Course.

0 votes
by (106k points)

You can use below-mentioned code for extracting extension from filename in Python:-

import os.path

extension = os.path.splitext(filename)

You can use the following video tutorials to clear all your doubts:-

Related questions

+2 votes
3 answers
+3 votes
2 answers
+3 votes
3 answers

Browse Categories

...