How to get the file extension from a filename in Python?

How to get the file extension from a filename in Python?

For example, I would like to get ".txt" from "file.txt".

asked Oct 15, 2015 by Eric Z Ma (44,280 points)

1 Answer

 
Best answer

The Python code:

>>> name, ext = os.path.splitext('file.txt')
>>> name
'file'
>>> ext
'.txt'

Manual of os.path.splitext: https://docs.python.org/2/library/os.path.html#os.path.splitext

answered Oct 15, 2015 by Eric Z Ma (44,280 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...