list comprehension to do os.path.split_all ?

Stefaan Himpe stefaan.himpe at
Thu Jul 28 22:32:17 CEST 2011


> [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/'  -->   ['home', 'gyoung',
> 'hacks', 'pathhack', '']

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

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

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

Best regards,

More information about the Python-list mailing list