Parsing a path to components

Discussion in 'Python' started by eliben, Jun 7, 2008.

  1. eliben

    eliben Guest

    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
    eliben, Jun 7, 2008
    #1
    1. Advertising

  2. eliben

    Guest

    On Jun 7, 12:55 am, eliben <> 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
    , Jun 7, 2008
    #2
    1. Advertising

  3. 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
    Marc 'BlackJack' Rintsch, Jun 7, 2008
    #3
  4. eliben

    eliben Guest

    On Jun 7, 10:15 am, Marc 'BlackJack' Rintsch <> 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
    eliben, Jun 7, 2008
    #4
  5. eliben

    Guest

    On Jun 7, 3:15 am, Marc 'BlackJack' Rintsch <> 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
    , Jun 7, 2008
    #5
  6. On Sat, 07 Jun 2008 02:15:07 -0700, s0suk3 wrote:

    > On Jun 7, 3:15 am, Marc 'BlackJack' Rintsch <> 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
    Marc 'BlackJack' Rintsch, Jun 7, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    863
    GIMME
    Feb 11, 2004
  2. Mickey Segal
    Replies:
    0
    Views:
    855
    Mickey Segal
    Feb 2, 2004
  3. Naren
    Replies:
    0
    Views:
    567
    Naren
    May 11, 2004
  4. Christopher Diggins
    Replies:
    0
    Views:
    597
    Christopher Diggins
    Jul 9, 2007
  5. Aaron Gray

    Getting path components

    Aaron Gray, Apr 21, 2008, in forum: C Programming
    Replies:
    17
    Views:
    470
    Kenneth Brody
    Apr 22, 2008
Loading...

Share This Page