Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Parsing a path to components

Reply
Thread Tools

Parsing a path to components

 
 
eliben
Guest
Posts: n/a
 
      06-07-2008
Hello,

os.path.split returns the head and tail of a path, but what if I want
to have all the components ? I could not find a portable way to do
this in the standard library, so I've concocted the following
function. It uses os.path.split to be portable, at the expense of
efficiency.

----------------------------------
def parse_path(path):
""" Parses a path to its components.

Example:
parse_path("C:\\Python25\\lib\\site-packages\
\zipextimporter.py")

Returns:
['C:\\', 'Python25', 'lib', 'site-packages',
'zipextimporter.py']

This function uses os.path.split in an attempt to be portable.
It costs in performance.
"""
lst = []

while 1:
head, tail = os.path.split(path)

if tail == '':
if head != '': lst.insert(0, head)
break
else:
lst.insert(0, tail)
path = head

return lst
----------------------------------

Did I miss something and there is a way to do this standardly ?
Is this function valid, or will there be cases that will confuse it ?

Thanks in advance
Eli
 
Reply With Quote
 
 
 
 
s0suk3@gmail.com
Guest
Posts: n/a
 
      06-07-2008
On Jun 7, 12:55*am, eliben <(E-Mail Removed)> wrote:
> Hello,
>
> os.path.split returns the head and tail of a path, but what if I want
> to have all the components ? I could not find a portable way to do
> this in the standard library, so I've concocted the following
> function. It uses os.path.split to be portable, at the expense of
> efficiency.
>
> ----------------------------------
> def parse_path(path):
> * * """ Parses a path to its components.
>
> * * * * Example:
> * * * * * * parse_path("C:\\Python25\\lib\\site-packages\
> \zipextimporter.py")
>
> * * * * * * Returns:
> * * * * * * ['C:\\', 'Python25', 'lib', 'site-packages',
> 'zipextimporter.py']
>
> * * * * This function uses os.path.split in an attempt to be portable.
> * * * * It costs in performance.
> * * """
> * * lst = []
>
> * * while 1:
> * * * * head, tail = os.path.split(path)
>
> * * * * if tail == '':
> * * * * * * if head != '': lst.insert(0, head)
> * * * * * * break
> * * * * else:
> * * * * * * lst.insert(0, tail)
> * * * * * * path = head
>
> * * return lst
> ----------------------------------
>
> Did I miss something and there is a way to do this standardly ?
> Is this function valid, or will there be cases that will confuse it ?
>


You can just split the path on `os.sep', which contains the path
separator of the platform on which Python is running:

components = pathString.split(os.sep)

Sebastian

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      06-07-2008
On Fri, 06 Jun 2008 23:57:03 -0700, s0suk3 wrote:

> You can just split the path on `os.sep', which contains the path
> separator of the platform on which Python is running:
>
> components = pathString.split(os.sep)


Won't work for platforms with more than one path separator and if a
separator is repeated. For example r'\foo\\bar/baz//spam.py' or:

In [140]: os.path.split('foo//bar')
Out[140]: ('foo', 'bar')

In [141]: 'foo//bar'.split(os.sep)
Out[141]: ['foo', '', 'bar']

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
eliben
Guest
Posts: n/a
 
      06-07-2008
On Jun 7, 10:15 am, Marc 'BlackJack' Rintsch <(E-Mail Removed)> wrote:
> On Fri, 06 Jun 2008 23:57:03 -0700, s0suk3 wrote:
> > You can just split the path on `os.sep', which contains the path
> > separator of the platform on which Python is running:

>
> > components = pathString.split(os.sep)

>
> Won't work for platforms with more than one path separator and if a
> separator is repeated. For example r'\foo\\bar/baz//spam.py' or:
>
> In [140]: os.path.split('foo//bar')
> Out[140]: ('foo', 'bar')
>
> In [141]: 'foo//bar'.split(os.sep)
> Out[141]: ['foo', '', 'bar']
>
> Ciao,
> Marc 'BlackJack' Rintsch


Can you recommend a generic way to achieve this ?
Eli
 
Reply With Quote
 
s0suk3@gmail.com
Guest
Posts: n/a
 
      06-07-2008
On Jun 7, 3:15*am, Marc 'BlackJack' Rintsch <(E-Mail Removed)> wrote:
> On Fri, 06 Jun 2008 23:57:03 -0700, s0suk3 wrote:
> > You can just split the path on `os.sep', which contains the path
> > separator of the platform on which Python is running:

>
> > components = pathString.split(os.sep)

>
> Won't work for platforms with more than one path separator and if a
> separator is repeated. *For example r'\foo\\bar/baz//spam.py' or:
>
> In [140]: os.path.split('foo//bar')
> Out[140]: ('foo', 'bar')
>
> In [141]: 'foo//bar'.split(os.sep)
> Out[141]: ['foo', '', 'bar']
>


But those are invalid paths, aren't they? If you have a jumble of a
path, I think the solution is to call os.path.normpath() before
splitting.

Sebastian
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      06-07-2008
On Sat, 07 Jun 2008 02:15:07 -0700, s0suk3 wrote:

> On Jun 7, 3:15*am, Marc 'BlackJack' Rintsch <(E-Mail Removed)> wrote:
>> On Fri, 06 Jun 2008 23:57:03 -0700, s0suk3 wrote:
>> > You can just split the path on `os.sep', which contains the path
>> > separator of the platform on which Python is running:

>>
>> > components = pathString.split(os.sep)

>>
>> Won't work for platforms with more than one path separator and if a
>> separator is repeated. *For example r'\foo\\bar/baz//spam.py' or:
>>
>> In [140]: os.path.split('foo//bar')
>> Out[140]: ('foo', 'bar')
>>
>> In [141]: 'foo//bar'.split(os.sep)
>> Out[141]: ['foo', '', 'bar']
>>

>
> But those are invalid paths, aren't they?


No. See `os.altsep` on Windows. And repeating separators is allowed too.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting path components Aaron Gray C Programming 17 04-22-2008 06:52 PM
SWING components adjustment in different resolutions - Should show scrollbars less than 800X600 and expand components over this resolution Bluetears76 Java 1 07-01-2004 09:01 PM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM
Can Choice components respond to keyboard input like HTML Choice components? Mickey Segal Java 0 02-02-2004 10:59 PM



Advertisments