list comprehension to do os.path.split_all ?

Ian Kelly ian.g.kelly at gmail.com
Thu Jul 28 16:47:42 EDT 2011


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.



More information about the Python-list mailing list