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

زياد بن زياد بن
Thu Jul 20 11:17:32 CEST 2006


On Thu, 2006-07-20 at 11:19 +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
> 
> Regards,
> Emily
> 
The following maybe a little better:
        def getExt(fname):
        	return fname.rsplit('.', 1)[1]
                
It uses one method instead of two compared to yours.  And in your code
there's no need for the ‘ext’ variable, you could use:
        def getext(fname):
        	return fname.split('.').pop()

For more information about ‘str’ objects type the following inside the
Python shell:
	>>> help(str)


Ziyad.



More information about the Tutor mailing list