views:

5313

answers:

2

Is there a function to extract the extension from a filename?

Thanks all! That's what I needed!

+36  A: 

Yes. use os.path.splitext.

>>> import os
>>> basename, extension = os.path.splitext('/path/to/somefile.ext')
>>> basename
'/path/to/somefile'
>>> extension
'.ext'
nosklo
+9  A: 
import os.path
extension = os.path.splitext(filename)[1]
Brian Neal
+1: I use this when I don't want the basename
nosklo