Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10512

Re: list comprehension to do os.path.split_all ?

From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Re: list comprehension to do os.path.split_all ?
Date 2011-07-29 12:40 +0000
Organization Norwich University
Message-ID <99fo1mFqeeU6@mid.individual.net> (permalink)
References <1954d20a-7177-45d9-afa0-e98b171b2822@w27g2000yqk.googlegroups.com> <j0sjig$5dj$1@dough.gmane.org> <CALwzidkv93zGmY7M0CjZ=cGsQfLnY-J5eiirruR4G4sd_DJw_w@mail.gmail.com> <mailman.1603.1311912962.1164.python-list@python.org>

Show all headers | View raw


On 2011-07-29, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
> On Thu, 28 Jul 2011 15:31:43 -0600, Ian Kelly
> <ian.g.kelly@gmail.com> declaimed the following in
> gmane.comp.python.general:
>
>> Using os.sep doesn't make it cross-platform. On Windows:
>> 
>> >>> os.path.split(r'C:\windows')
>> ('C:\\', 'windows')
>> >>> os.path.split(r'C:/windows')
>> ('C:/', 'windows')
>> >>> r'C:\windows'.split(os.sep)
>> ['C:', 'windows']
>> >>> r'C:/windows'.split(os.sep)
>> ['C:/windows']
>
> 	Fine... So normpath it first...
>
>>>> os.path.normpath(r'C:/windows').split(os.sep)
> ['C:', 'windows']
>>>> 

Here's a solution adapted from an initially recursive attempt.
The tests are currently somewhat gnarled to avoid displaying
os.path.sep. A simpler solution probably reimplement os.path.split,
an inconvenient implementation detail.

import os

def split_path(path):
    """Split path into a series of directory names, and return it
    as a list.

    If path is absolute, the first element in the list will be be
    os.path.sep.

    >>> p = split_path('/smith/jones')
    >>> p[0] == os.path.sep
    True
    >>> p[1:]
    ['smith', 'jones']

    >>> split_path('smith/jones')
    ['smith', 'jones']

    >>> split_path('')
    []

    >>> p = split_path('/')
    >>> p[0] == os.path.sep
    True
    >>> len(p)
    1
    """
    head, tail = os.path.split(path)
    retval = []
    while tail != '':
        retval.append(tail)
        head, tail = os.path.split(head)
    else:
        if os.path.isabs(path):
            retval.append(os.path.sep)
        return list(reversed(retval))

if __name__ == '__main__':
    import doctest
    doctest.testmod()

-- 
Neil Cerutti

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

list comprehension to do os.path.split_all ? gry <georgeryoung@gmail.com> - 2011-07-28 13:18 -0700
  Re: list comprehension to do os.path.split_all ? Stefaan Himpe <stefaan.himpe@gmail.com> - 2011-07-28 22:32 +0200
  Re: list comprehension to do os.path.split_all ? Neil Cerutti <neilc@norwich.edu> - 2011-07-28 20:36 +0000
    Re: list comprehension to do os.path.split_all ? Ethan Furman <ethan@stoneleaf.us> - 2011-07-28 14:03 -0700
  Re: list comprehension to do os.path.split_all ? Alan Meyer <ameyer2@yahoo.com> - 2011-07-28 16:40 -0400
    Re: list comprehension to do os.path.split_all ? TheSaint <no@nowhere.net.no> - 2011-07-29 22:29 +0800
  Re: list comprehension to do os.path.split_all ? Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-28 14:44 -0600
  Re: list comprehension to do os.path.split_all ? Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-28 14:47 -0600
  Re: list comprehension to do os.path.split_all ? Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-28 15:04 -0600
  Re: list comprehension to do os.path.split_all ? Alexander Kapps <alex.kapps@web.de> - 2011-07-28 23:06 +0200
  Re: list comprehension to do os.path.split_all ? Emile van Sebille <emile@fenx.com> - 2011-07-28 14:15 -0700
  Re: list comprehension to do os.path.split_all ? Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-28 15:31 -0600
    Re: list comprehension to do os.path.split_all ? Carl Banks <pavlovevidence@gmail.com> - 2011-07-29 12:30 -0700
      Re: list comprehension to do os.path.split_all ? Alexander Kapps <alex.kapps@web.de> - 2011-07-29 22:22 +0200
      Re: list comprehension to do os.path.split_all ? Michael Poeltl <michael.poeltl@univie.ac.at> - 2011-07-30 02:23 +0200
      Re: list comprehension to do os.path.split_all ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-30 10:55 +1000
      Re: list comprehension to do os.path.split_all ? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-07-29 22:17 -0700
      Re: list comprehension to do os.path.split_all ? Michael Poeltl <michael.poeltl@univie.ac.at> - 2011-07-31 07:40 +0200
  Re: list comprehension to do os.path.split_all ? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-07-28 21:15 -0700
    Re: list comprehension to do os.path.split_all ? Neil Cerutti <neilc@norwich.edu> - 2011-07-29 12:40 +0000
      Re: list comprehension to do os.path.split_all ? Hrvoje Niksic <hniksic@xemacs.org> - 2011-07-31 08:42 +0200

csiph-web