Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10470 > unrolled thread
| Started by | gry <georgeryoung@gmail.com> |
|---|---|
| First post | 2011-07-28 13:18 -0700 |
| Last post | 2011-07-31 08:42 +0200 |
| Articles | 20 on this page of 21 — 14 participants |
Back to article view | Back to comp.lang.python
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
Page 1 of 2 [1] 2 Next page →
| From | gry <georgeryoung@gmail.com> |
|---|---|
| Date | 2011-07-28 13:18 -0700 |
| Subject | list comprehension to do os.path.split_all ? |
| Message-ID | <1954d20a-7177-45d9-afa0-e98b171b2822@w27g2000yqk.googlegroups.com> |
[python 2.7] I have a (linux) pathname that I'd like to split completely into a list of components, e.g.: '/home/gyoung/hacks/pathhack/foo.py' --> ['home', 'gyoung', 'hacks', 'pathhack', 'foo.py'] os.path.split gives me a tuple of dirname,basename, but there's no os.path.split_all function. I expect I can do this with some simple loop, but I have such faith in the wonderfulness of list comprehensions, that it seems like there should be a way to use them for an elegant solution of my problem. I can't quite work it out. Any brilliant ideas? (or other elegant solutions to the problem?) -- George
[toc] | [next] | [standalone]
| From | Stefaan Himpe <stefaan.himpe@gmail.com> |
|---|---|
| Date | 2011-07-28 22:32 +0200 |
| Message-ID | <lHjYp.93392$2I6.43394@newsfe27.ams2> |
| In reply to | #10470 |
Hi,
> [python 2.7] I have a (linux) pathname that I'd like to split
> completely into a list of components, e.g.:
> '/home/gyoung/hacks/pathhack/foo.py' --> ['home', 'gyoung',
> 'hacks', 'pathhack', 'foo.py']
Not sure what your exact requirements are, but the following seems to work:
pathname = '/home/gyoung/hacks/pathhack/foo.py'
print pathname[1:].split("/")
Note that this would only work for absolute linux paths (i.e. starting
with "/").
Best regards,
Stefaan.
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2011-07-28 20:36 +0000 |
| Message-ID | <99dvipF12mU1@mid.individual.net> |
| In reply to | #10470 |
On 2011-07-28, gry <georgeryoung@gmail.com> wrote: > [python 2.7] I have a (linux) pathname that I'd like to split > completely into a list of components, e.g.: > '/home/gyoung/hacks/pathhack/foo.py' --> ['home', 'gyoung', > 'hacks', 'pathhack', 'foo.py'] > > os.path.split gives me a tuple of dirname,basename, but there's > no os.path.split_all function. > > I expect I can do this with some simple loop, but I have such > faith in the wonderfulness of list comprehensions, that it > seems like there should be a way to use them for an elegant > solution of my problem. I can't quite work it out. Any > brilliant ideas? (or other elegant solutions to the problem?) If an elegant solution doesn't occur to me right away, then I first compose the most obvious solution I can think of. Finally, I refactor it until elegance is either achieved or imagined. -- Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2011-07-28 14:03 -0700 |
| Message-ID | <mailman.1589.1311886111.1164.python-list@python.org> |
| In reply to | #10472 |
Neil Cerutti wrote: > If an elegant solution doesn't occur to me right away, then I > first compose the most obvious solution I can think of. Finally, > I refactor it until elegance is either achieved or imagined. +1 QOTW
[toc] | [prev] | [next] | [standalone]
| From | Alan Meyer <ameyer2@yahoo.com> |
|---|---|
| Date | 2011-07-28 16:40 -0400 |
| Message-ID | <4E31C959.9050606@yahoo.com> |
| In reply to | #10470 |
On 7/28/2011 4:18 PM, gry wrote:
> [python 2.7] I have a (linux) pathname that I'd like to split
> completely into a list of components, e.g.:
> '/home/gyoung/hacks/pathhack/foo.py' --> ['home', 'gyoung',
> 'hacks', 'pathhack', 'foo.py']
>
> os.path.split gives me a tuple of dirname,basename, but there's no
> os.path.split_all function.
>
> I expect I can do this with some simple loop, but I have such faith in
> the wonderfulness of list comprehensions, that it seems like there
> should be a way to use them for an elegant solution of my problem.
> I can't quite work it out. Any brilliant ideas? (or other elegant
> solutions to the problem?)
>
> -- George
This is not properly portable to all OS, but you could simply split on
the slash character, e.g.,
pathname.split('/')
Alan
[toc] | [prev] | [next] | [standalone]
| From | TheSaint <no@nowhere.net.no> |
|---|---|
| Date | 2011-07-29 22:29 +0800 |
| Message-ID | <j0ug3v$u4b$1@speranza.aioe.org> |
| In reply to | #10473 |
Alan Meyer wrote:
> This is not properly portable to all OS, but you could simply split on
> the slash character, e.g.,
>
> pathname.split('/')
more portable pathname.split(os.sep)
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-07-28 14:44 -0600 |
| Message-ID | <mailman.1586.1311885887.1164.python-list@python.org> |
| In reply to | #10470 |
On Thu, Jul 28, 2011 at 2:18 PM, gry <georgeryoung@gmail.com> wrote:
> [python 2.7] I have a (linux) pathname that I'd like to split
> completely into a list of components, e.g.:
> '/home/gyoung/hacks/pathhack/foo.py' --> ['home', 'gyoung',
> 'hacks', 'pathhack', 'foo.py']
>
> os.path.split gives me a tuple of dirname,basename, but there's no
> os.path.split_all function.
>
> I expect I can do this with some simple loop, but I have such faith in
> the wonderfulness of list comprehensions, that it seems like there
> should be a way to use them for an elegant solution of my problem.
> I can't quite work it out. Any brilliant ideas? (or other elegant
> solutions to the problem?)
path = '/home/gyoung/hacks/pathhack/foo.py'
parts = [part for path, part in iter(lambda: os.path.split(path), ('/', ''))]
parts.reverse()
print parts
But that's horrendously ugly. Just write a generator with a while
loop or something.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-07-28 14:47 -0600 |
| Message-ID | <mailman.1588.1311886094.1164.python-list@python.org> |
| In reply to | #10470 |
On Thu, Jul 28, 2011 at 2:44 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> path = '/home/gyoung/hacks/pathhack/foo.py'
> parts = [part for path, part in iter(lambda: os.path.split(path), ('/', ''))]
> parts.reverse()
> print parts
>
> But that's horrendously ugly. Just write a generator with a while
> loop or something.
Also, note that if the path does not start with '/', the result will
be an infinite loop.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-07-28 15:04 -0600 |
| Message-ID | <mailman.1590.1311887124.1164.python-list@python.org> |
| In reply to | #10470 |
On Thu, Jul 28, 2011 at 2:47 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Thu, Jul 28, 2011 at 2:44 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>> path = '/home/gyoung/hacks/pathhack/foo.py'
>> parts = [part for path, part in iter(lambda: os.path.split(path), ('/', ''))]
>> parts.reverse()
>> print parts
>>
>> But that's horrendously ugly. Just write a generator with a while
>> loop or something.
>
> Also, note that if the path does not start with '/', the result will
> be an infinite loop.
Oh, and this won't work at all in Python 3, because it relies on the
lambda having access to the list comprehension scope.
[toc] | [prev] | [next] | [standalone]
| From | Alexander Kapps <alex.kapps@web.de> |
|---|---|
| Date | 2011-07-28 23:06 +0200 |
| Message-ID | <mailman.1591.1311887224.1164.python-list@python.org> |
| In reply to | #10470 |
On 28.07.2011 22:44, Ian Kelly wrote:
> On Thu, Jul 28, 2011 at 2:18 PM, gry<georgeryoung@gmail.com> wrote:
>> [python 2.7] I have a (linux) pathname that I'd like to split
>> completely into a list of components, e.g.:
>> '/home/gyoung/hacks/pathhack/foo.py' --> ['home', 'gyoung',
>> 'hacks', 'pathhack', 'foo.py']
>>
>> os.path.split gives me a tuple of dirname,basename, but there's no
>> os.path.split_all function.
>>
>> I expect I can do this with some simple loop, but I have such faith in
>> the wonderfulness of list comprehensions, that it seems like there
>> should be a way to use them for an elegant solution of my problem.
>> I can't quite work it out. Any brilliant ideas? (or other elegant
>> solutions to the problem?)
>
> path = '/home/gyoung/hacks/pathhack/foo.py'
> parts = [part for path, part in iter(lambda: os.path.split(path), ('/', ''))]
> parts.reverse()
> print parts
>
> But that's horrendously ugly. Just write a generator with a while
> loop or something.
pathname = '/home/gyoung/hacks/pathhack/foo.py'
parts = [part for part in pathname.split(os.path.sep) if part]
print parts
['home', 'gyoung', 'hacks', 'pathhack', 'foo.py']
[toc] | [prev] | [next] | [standalone]
| From | Emile van Sebille <emile@fenx.com> |
|---|---|
| Date | 2011-07-28 14:15 -0700 |
| Message-ID | <mailman.1593.1311887790.1164.python-list@python.org> |
| In reply to | #10470 |
On 7/28/2011 1:18 PM gry said... > [python 2.7] I have a (linux) pathname that I'd like to split > completely into a list of components, e.g.: > '/home/gyoung/hacks/pathhack/foo.py' --> ['home', 'gyoung', > 'hacks', 'pathhack', 'foo.py'] > > os.path.split gives me a tuple of dirname,basename, but there's no > os.path.split_all function. > Why not just split? '/home/gyoung/hacks/pathhack/foo.py'.split(os.sep) Emile
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-07-28 15:31 -0600 |
| Message-ID | <mailman.1595.1311888735.1164.python-list@python.org> |
| In reply to | #10470 |
On Thu, Jul 28, 2011 at 3:15 PM, Emile van Sebille <emile@fenx.com> wrote:
> On 7/28/2011 1:18 PM gry said...
>>
>> [python 2.7] I have a (linux) pathname that I'd like to split
>> completely into a list of components, e.g.:
>> '/home/gyoung/hacks/pathhack/foo.py' --> ['home', 'gyoung',
>> 'hacks', 'pathhack', 'foo.py']
>>
>> os.path.split gives me a tuple of dirname,basename, but there's no
>> os.path.split_all function.
>>
>
> Why not just split?
>
> '/home/gyoung/hacks/pathhack/foo.py'.split(os.sep)
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']
[toc] | [prev] | [next] | [standalone]
| From | Carl Banks <pavlovevidence@gmail.com> |
|---|---|
| Date | 2011-07-29 12:30 -0700 |
| Message-ID | <879a920c-d06a-4413-9566-6c9f0b737235@glegroupsg2000goo.googlegroups.com> |
| In reply to | #10483 |
On Thursday, July 28, 2011 2:31:43 PM UTC-7, Ian wrote:
> On Thu, Jul 28, 2011 at 3:15 PM, Emile van Sebille <em...@fenx.com> wrote:
> > On 7/28/2011 1:18 PM gry said...
> >>
> >> [python 2.7] I have a (linux) pathname that I'd like to split
> >> completely into a list of components, e.g.:
> >> '/home/gyoung/hacks/pathhack/foo.py' --> ['home', 'gyoung',
> >> 'hacks', 'pathhack', 'foo.py']
> >>
> >> os.path.split gives me a tuple of dirname,basename, but there's no
> >> os.path.split_all function.
> >>
> >
> > Why not just split?
> >
> > '/home/gyoung/hacks/pathhack/foo.py'.split(os.sep)
>
> 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']
It's not even fullproof on Unix.
'/home//h1122/bin///ghi/'.split('/')
['','home','','bin','','','ghi','']
The whole point of the os.path functions are to take care of whatever oddities there are in the path system. When you use string manipulation to manipulate paths, you bypass all of that and leave yourself open to those oddities, and then you find your applications break when a user enters a doubled slash.
So stick to os.path.
Carl Banks
[toc] | [prev] | [next] | [standalone]
| From | Alexander Kapps <alex.kapps@web.de> |
|---|---|
| Date | 2011-07-29 22:22 +0200 |
| Message-ID | <mailman.1627.1311971002.1164.python-list@python.org> |
| In reply to | #10534 |
On 29.07.2011 21:30, Carl Banks wrote:
> It's not even fullproof on Unix.
>
> '/home//h1122/bin///ghi/'.split('/')
>
> ['','home','','bin','','','ghi','']
>
> The whole point of the os.path functions are to take care of whatever oddities there are in the path system. When you use string manipulation to manipulate paths, you bypass all of that and leave yourself open to those oddities, and then you find your applications break when a user enters a doubled slash.
>
> So stick to os.path.
>
>
> Carl Banks
This would also be fixed with normpath() as Dennis Lee Bieber
suggested. And my solution with list comprehensions handles this too.
Still, there might be other path oddities which would break here. I
think, that something like a split_all() function should be
available in the stdlib, no?
Actually, it isn't the first time, where I wonder why
os.path.split() doesn't do this already. I mean, str.split() doesn't
only split on the first part, right?
[toc] | [prev] | [next] | [standalone]
| From | Michael Poeltl <michael.poeltl@univie.ac.at> |
|---|---|
| Date | 2011-07-30 02:23 +0200 |
| Message-ID | <mailman.1645.1311985785.1164.python-list@python.org> |
| In reply to | #10534 |
* Alexander Kapps <alex.kapps@web.de> [2011-07-29 22:30]:
> On 29.07.2011 21:30, Carl Banks wrote:
>
>> It's not even fullproof on Unix.
>>
>> '/home//h1122/bin///ghi/'.split('/')
>>
>> ['','home','','bin','','','ghi','']
what about this?
>>> ' '.join('/home//h1122/bin///ghi/'.split('/')).split()
['home', 'h1122', 'bin', 'ghi']
>>>
;-)
regards
Michael
>> Carl Banks
>
> This would also be fixed with normpath() as Dennis Lee Bieber suggested.
> And my solution with list comprehensions handles this too.
>
> Still, there might be other path oddities which would break here. I
> think, that something like a split_all() function should be available in
> the stdlib, no?
>
> Actually, it isn't the first time, where I wonder why os.path.split()
> doesn't do this already. I mean, str.split() doesn't only split on the
> first part, right?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
Michael Poeltl
Computational Materials Physics voice: +43-1-4277-51409
Univ. Wien, Sensengasse 8/12 fax: +43-1-4277-9514 (or 9513)
A-1090 Wien, AUSTRIA cmp.mpi.univie.ac.at
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-07-30 10:55 +1000 |
| Message-ID | <4e335693$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #10534 |
Carl Banks wrote:
> It's not even fullproof on Unix.
>
> '/home//h1122/bin///ghi/'.split('/')
>
> ['','home','','bin','','','ghi','']
What? No. Absolutely not -- that would be a major bug. Did you actually try
it?
>>> '/home//h1122/bin///ghi/'.split('/')
['', 'home', '', 'h1122', 'bin', '', '', 'ghi', '']
Exactly as expected, and correct.
Now obviously if you intend treating this as a list of path components, you
have to filter out the empty strings, but otherwise works fine.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2011-07-29 22:17 -0700 |
| Message-ID | <mailman.1651.1312003047.1164.python-list@python.org> |
| In reply to | #10534 |
On Fri, 29 Jul 2011 22:22:54 +0200, Alexander Kapps <alex.kapps@web.de>
declaimed the following in gmane.comp.python.general:
> Actually, it isn't the first time, where I wonder why
> os.path.split() doesn't do this already. I mean, str.split() doesn't
> only split on the first part, right?
But there is a conceptual difference... The last element of a path
is a file (or directory name) contained within the preceding path name
portion.
But a string has no context...
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Michael Poeltl <michael.poeltl@univie.ac.at> |
|---|---|
| Date | 2011-07-31 07:40 +0200 |
| Message-ID | <mailman.1687.1312090839.1164.python-list@python.org> |
| In reply to | #10534 |
* Michael Torrie <torriem@gmail.com> [2011-07-31 03:44]:
> On Jul 29, 2011 6:33 PM, "Michael Poeltl" <michael.poeltl@univie.ac.at>
> wrote:
> >
> > what about this?
> > >>> ' '.join('/home//h1122/bin///ghi/'.split('/')).split()
> > ['home', 'h1122', 'bin', 'ghi']
> > >>>
>
> Doesn't work on filenames with spaces in them.
you are right; me, I never put spaces into my filenames and so I didn't
think of this possibility.
so there is another idea, which will not work on dirnames 'with spacs in
them' ;-)
>>> p = '/path//to///file/i am a file'
>>> ' '.join(os.path.split(p)[0].split('/')).split().__add__([os.path.split(p)[1]])
['path', 'to', 'file', 'i am a file']
>>>
--
Michael Poeltl
Computational Materials Physics voice: +43-1-4277-51409
Univ. Wien, Sensengasse 8/12 fax: +43-1-4277-9514 (or 9513)
A-1090 Wien, AUSTRIA cmp.mpi.univie.ac.at
-----------------------------------------------------------------
slackware-12.2/ubuntu-10.10 | vim-7.3 | python-3.2.1 | mutt-1.5.18 | elinks-0.12
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2011-07-28 21:15 -0700 |
| Message-ID | <mailman.1603.1311912962.1164.python-list@python.org> |
| In reply to | #10470 |
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']
>>>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2011-07-29 12:40 +0000 |
| Message-ID | <99fo1mFqeeU6@mid.individual.net> |
| In reply to | #10498 |
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
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web