list comprehension to do os.path.split_all ?

Stefaan Himpe stefaan.himpe at gmail.com
Thu Jul 28 16:32:17 EDT 2011


Hi,

> [python 2.7] I have a (linux) pathname that I'd like to split
> completely into a list of components, e.g.:
>     '/home/gyoung/hacks/pathhack/foo.py'  -->   ['home', 'gyoung',
> 'hacks', 'pathhack', 'foo.py']

Not sure what your exact requirements are, but the following seems to work:

pathname = '/home/gyoung/hacks/pathhack/foo.py'
print pathname[1:].split("/")

Note that this would only work for absolute linux paths (i.e. starting 
with "/").

Best regards,
Stefaan.



More information about the Python-list mailing list