is there a nmore pythonic way ?

Terry Reedy tjreedy at udel.edu
Tue Mar 11 15:57:24 EST 2003


"A. Lloyd Flanagan" <alloydflanagan at attbi.com> wrote in message
news:a8b7f07a.0303110900.3fb68397 at posting.google.com...
> pos = sys.argv[1].rfind('.')
> im, func = sys.argv[1][:pos], sys.argv[1][pos+1:]

tem = sys.argv[1]
pos = tem.rfind('.')
if pos != -1:
  im,func = tem[:pos], tem[pos+1:]
>
> I don't know if that's more pythonic, but I found it a lot more
> readable than using split and join in this case.

Definitely.  Factoring out the .attr[sub] is even more readable to me.

Alternative (initially leaves func with leading '.'):
import os
im, func = os.path.splitext(sys.argv[1])
func = func[1:]

Example:
>>> s = os.path.splitext
>>> s('abc')
('abc', '')
>>> s('ab.c')
('ab', '.c')
>>> s('a.b.c')
('a.b', '.c')

Terry J. Reedy






More information about the Python-list mailing list