473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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','alc ohol']

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?

Jul 18 '05 #1
1 2834
po*********@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','alc ohol']
'/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?

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
5
by: oliver | last post by:
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a...
6
by: Pat | last post by:
Hello, Two questions - hence the weird subject. First, anyone had luck with the Enumerating user accounts in a NT Domain module by Dev Ashish on Access Web...
3
by: John Salerno | last post by:
This is an example in the book I'm reading: string fullName = " Edward C Koop "; fullName = fullName.Trim(); string names = fullName.Split(' '); string firstName = names; //...
3
by: David Pratt | last post by:
Hi. I am splitting a string on a non whitespace character. One or more whitespace characters can be returned as items in the list. I do not want the items in the list that are only whitespace (can...
4
by: Michele Petrazzo | last post by:
Hello ng, I don't understand why split (string split) doesn't work with the same method if I can't pass values or if I pass a whitespace value: >>> "".split() >>> "".split(" ") But into...
5
by: sck10 | last post by:
Hello, I have a list of email addresses that I need to send email to from the website. I am trying to use the "Split" function to get all the To's and then use the uBound function for the...
8
by: Wijaya Edward | last post by:
Hi all, I was trying to split a string that represent chinese characters below: ??? But why the split function here doesn't seem
2
by: Shawn Minisall | last post by:
I'm trying to unpack a list of 5 floats from a list read from a file and python is telling me 5 variables are too many for the string.split statement. Anyone have any other idea's? NOTE: the only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.