split a directory string into a list

Harper, Gina gharper at medplus.com
Fri Feb 25 09:09:23 EST 2005


I would start with something like this:
somestring = '/foo/bar/beer/sex/cigarettes/drugs/alcohol/'
somelist = somestring.split('/')
print somelist
This is close to what you seem to want.  Good luck.
*gina*

-----Original Message-----
From: porterboy76 at yahoo.com [mailto:porterboy76 at yahoo.com] 
Sent: Friday, February 25, 2005 8:36 AM
To: python-list at python.org
Subject: split a directory string into a list


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