split a directory string into a list

porterboy76 at yahoo.com porterboy76 at yahoo.com
Fri Feb 25 08:36:23 EST 2005


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']

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