[Tutor] [tutor] how to get the fileextention?

Michael Lange klappnase at freenet.de
Thu Jul 20 11:11:53 CEST 2006


On Thu, 20 Jul 2006 11:19:46 +0300
emilia12 at mail.bg wrote:

> Hi,
> 
> is this the right (shortest) way to get the file extention
> (under MS WIN)?
> 
> 
> def getext(fname):
>     ext = fname.split('.').pop()
>     return ext
> 

Hi Emily,

for filename operations, you should have a look at the os.path module.
In your case

    os.path.splitext(fname)[1]

should do the trick, or if you need to remove the leading period from the extension
(as in your example)

    os.path.splitext(fname)[1][1:]
.

I hope this helps

Michael


More information about the Tutor mailing list