How to get the extension of a filename from the path

Tom Anderson twic at urchin.earth.li
Thu Dec 8 06:46:46 EST 2005


On Thu, 8 Dec 2005, Lad wrote:

> what is a way to get the the extension of  a filename from the path?
> E.g., on my XP windows the path can be
> C:\Pictures\MyDocs\test.txt
> and I would like to get
> the the extension of  the filename, that is here
> txt

You want os.path.splitext:

>>> import os
>>> os.path.splitext("C:\Pictures\MyDocs\test.txt")
('C:\\Pictures\\MyDocs\test', '.txt')
>>> os.path.splitext("C:\Pictures\MyDocs\test.txt")[1]
'.txt'
>>>

> I would like that to work on Linux also

It'll be fine.

tom

-- 
[Philosophy] is kind of like being driven behind the sofa by Dr Who -
scary, but still entertaining. -- Itchyfidget



More information about the Python-list mailing list