What does this line of code mean?

Peter Otten __peter__ at web.de
Sun Nov 16 16:59:01 EST 2014


Abdul Abdul wrote:

> I just came across the following line of code:
> 
> outputfile = os.path.splitext(infile)[0] + ".jpg"
> 
> Can you kindly explain to me what those parts mean?

You can try it yourself in the interactive interpreter:

>>> import os.path
>>> help(os.path.splitext)
Help on function splitext in module ntpath:

splitext(p)
    Split the extension from a pathname.
    
    Extension is everything from the last dot to the end, ignoring
    leading dots.  Returns "(root, ext)"; ext may be empty.

>>> infile = r"c:\some\folder\image.png"
>>> os.path.splitext(infile)
('c:\\some\\folder\\image', '.png')
>>> os.path.splitext(infile)[0]
'c:\\some\\folder\\image'
>>> os.path.splitext(infile)[0] + ".jpg"
'c:\\some\\folder\\image.jpg'

PS: I'm using Linux; If you look close enough at the above session you'll 
note that I've cheated a bit.




More information about the Python-list mailing list