Re: split a directory string into a list

2005-02-28 Thread Josef Meile
The most obvious case where it wouldn't work would be for a UNC path name. 
Using the string split method gives two empty strings:


os.path.normpath(r'\\machine\share').split(os.path.sep)
['', '', 'machine', 'share']

whereas the splitpath function I proposed gives you:

splitpath(r'\\machine\share')
['', 'machine', 'share']
So to find out the type of path (relative, absolute, unc), you only have to 
consider the first element with my function but you have to look at the 
first two elements if you just naively split the string.
Thanks for the explanation. I forgot that you could do thinks like that
on windows.
Also a relative windows path with a drive letter doesn't get fully split:

os.path.normpath(r'c:dir\file').split(os.path.sep)
['c:dir', 'file']
splitpath(r'c:dir\file')
['c:', 'dir', 'file']
Again, I forgot it.
If you really are worried about speed (and are sure you aren't optimising 
prematurely), then you could combine some special case processing near the 
start of the string with a simple split of the remainder.
No, I'm not worried about speed. Actually, the original post wasn't
mine. I was just curious about your answer.
Regards,
Josef
--
http://mail.python.org/mailman/listinfo/python-list


Re: split a directory string into a list

2005-02-26 Thread Duncan Booth
Josef Meile wrote:

 This should work ***reasonably*** reliably on Windows and Unix. Are
 there any cases when it does not work?

The most obvious case where it wouldn't work would be for a UNC path name. 
Using the string split method gives two empty strings:

 os.path.normpath(r'\\machine\share').split(os.path.sep)
['', '', 'machine', 'share']
 


whereas the splitpath function I proposed gives you:

 splitpath(r'\\machine\share')
['', 'machine', 'share']

So to find out the type of path (relative, absolute, unc), you only have to 
consider the first element with my function but you have to look at the 
first two elements if you just naively split the string.

Also a relative windows path with a drive letter doesn't get fully split:

 os.path.normpath(r'c:dir\file').split(os.path.sep)
['c:dir', 'file']
 splitpath(r'c:dir\file')
['c:', 'dir', 'file']

If you really are worried about speed (and are sure you aren't optimising 
prematurely), then you could combine some special case processing near the 
start of the string with a simple split of the remainder.
-- 
http://mail.python.org/mailman/listinfo/python-list


split a directory string into a list

2005-02-25 Thread porterboy76
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?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: split a directory string into a list

2005-02-25 Thread Kent Johnson
[EMAIL PROTECTED] 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?
--
http://mail.python.org/mailman/listinfo/python-list


RE: split a directory string into a list

2005-02-25 Thread Harper, Gina
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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: Friday, February 25, 2005 8:36 AM
To: python-list@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?


--
http://mail.python.org/mailman/listinfo/python-list


Re: split a directory string into a list

2005-02-25 Thread Michael Maibaum
On 25 Feb 2005, at 14:09, Harper, Gina wrote:
I would start with something like this:
somestring = '/foo/bar/beer/sex/cigarettes/drugs/alcohol/'
somelist = somestring.split('/')
print somelist
However - this will not work on Windows. It'd work on all the OS I 
usually use though ;)


Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: split a directory string into a list

2005-02-25 Thread Duncan Booth
Michael Maibaum wrote:

 On 25 Feb 2005, at 14:09, Harper, Gina wrote:
 
 I would start with something like this:
 somestring = '/foo/bar/beer/sex/cigarettes/drugs/alcohol/'
 somelist = somestring.split('/')
 print somelist
 
 However - this will not work on Windows. It'd work on all the OS I 
 usually use though ;)
 

This should work reasonably reliably on Windows and Unix:

 somestring = '/foo/bar/beer/sex/cigarettes/drugs/alcohol/'
 os.path.normpath(somestring).split(os.path.sep)
['', 'foo', 'bar', 'beer', 'sex', 'cigarettes', 'drugs', 'alcohol']

However a better solution is probably to call os.path.split repeatedly 
until it won't split anything more:

 somestring = r'C:\foo\bar\beer'
 def splitpath(p):
res = []
while 1:
p, file = os.path.split(p)
if not file:
break
res.append(file)
res.append(p)
res.reverse()
return res

 splitpath(somestring)
['C:\\', 'foo', 'bar', 'beer']
 splitpath('foo/bar')
['', 'foo', 'bar']

The first component is an empty string for relative paths, a drive letter 
or \ for absolute Windows paths, \\ for UNC paths, / for unix absolute 
paths.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: split a directory string into a list

2005-02-25 Thread Josef Meile
Hi Duncan,
This should work reasonably reliably on Windows and Unix:

somestring = '/foo/bar/beer/sex/cigarettes/drugs/alcohol/'
os.path.normpath(somestring).split(os.path.sep)
['', 'foo', 'bar', 'beer', 'sex', 'cigarettes', 'drugs', 'alcohol']
However a better solution is probably to call os.path.split repeatedly 
until it won't split anything more:


somestring = r'C:\foo\bar\beer'
def splitpath(p):
res = []
while 1:
p, file = os.path.split(p)
if not file:
break
res.append(file)
res.append(p)
res.reverse()
return res

splitpath(somestring)
['C:\\', 'foo', 'bar', 'beer']
splitpath('foo/bar')
['', 'foo', 'bar']
The first component is an empty string for relative paths, a drive letter 
or \ for absolute Windows paths, \\ for UNC paths, / for unix absolute 
paths.
I don't understand why the second approach is better than the first
one. In my opinion it is the contrary:
On the first approach, you only scan the string twice:
when you do os.path.normpath(somestring) and finally when splitting
the string. On the other hand, the second approach goes through the
string several times, when doing the os.path.split. Finally when doing
the res.reverse(). Or am I wrong? I also saw you said:
This should work ***reasonably*** reliably on Windows and Unix. Are
there any cases when it does not work?
Regards,
Josef
--
http://mail.python.org/mailman/listinfo/python-list