list comprehension to do os.path.split_all ?

Ian Kelly ian.g.kelly at gmail.com
Thu Jul 28 23:04:52 CEST 2011


On Thu, Jul 28, 2011 at 2:47 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Thu, Jul 28, 2011 at 2:44 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> path = '/home/gyoung/hacks/pathhack/foo.py'
>> parts = [part for path, part in iter(lambda: os.path.split(path), ('/', ''))]
>> parts.reverse()
>> print parts
>>
>> But that's horrendously ugly.  Just write a generator with a while
>> loop or something.
>
> Also, note that if the path does not start with '/', the result will
> be an infinite loop.

Oh, and this won't work at all in Python 3, because it relies on the
lambda having access to the list comprehension scope.



More information about the Python-list mailing list