How to get the extension of a filename from the path
gene tani
gene.tani at gmail.com
Thu Dec 8 07:26:02 EST 2005
Lad wrote:
> Hello,
> 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
>
>
> I would like that to work on Linux also
> Thank you for help
> L.
minor footnote: windows paths can be raw strings for os.path.split(),
or you can escape "/"
tho Tom's examp indicates unescaped, non-raw string works with
splitext()
import os.path
# winpath='C:\\Pictures\\MyDocs\\test.txt'
winpath=r'C:\Pictures\MyDocs\test.txt'
fpath,fname_ext=os.path.split(winpath)
print "path: %s ;;;;; fname and ext: %s"%(fpath, fname_ext)
ext=fname_ext.split(".")[-1]
print ext
More information about the Python-list
mailing list