+2 votes
2 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 (46.1k 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......!! 

0 votes
by (107k 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
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...