split a directory string into a list
Kent Johnson
kent37 at tds.net
Fri Feb 25 08:45:16 EST 2005
porterboy76 at yahoo.com wrote:
> QUESTION:
>
> How do I split a directory string into a list in Python, eg.
>
> '/foo/bar/beer/sex/cigarettes/drugs/alcohol/'
>
> becomes
>
> ['foo','bar','beer','sex','cigarettes','drugs','alcohol']
>>> '/foo/bar/beer/sex/cigarettes/drugs/alcohol/'.strip('/').split('/')
['foo', 'bar', 'beer', 'sex', 'cigarettes', 'drugs', 'alcohol']
Kent
>
> I was looking at the os.path.split command, but it only seems to
> separate the filename from the path (or am I just using it wrong?). I
> don't want to do it manually if I can help it, as there will have to be
> exceptions for the cases where there is (not) a trailing (leading)
> slash, or escape sequences involving /. Is there a built in command for
> this?
>
More information about the Python-list
mailing list