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


Groups > comp.lang.python > #10479

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <alex.kapps@web.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.030
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'path,': 0.07; 'brilliant': 0.09; 'tuple': 0.09; 'wrote:': 0.15; '2:18': 0.16; 'ideas?': 0.16; 'message-id:@web.de': 0.16; 'pathname': 0.16; 'ugly.': 0.16; 'pm,': 0.16; 'subject:list': 0.16; 'seems': 0.20; 'loop': 0.22; 'header:In-Reply-To:1': 0.22; 'elegant': 0.23; 'loop,': 0.23; 'expect': 0.25; '(or': 0.25; 'thu,': 0.28; "skip:' 30": 0.29; 'components,': 0.30; 'from:addr:web.de': 0.30; 'kelly': 0.30; 'print': 0.32; 'list': 0.32; 'to:addr:python-list': 0.34; 'header :User-Agent:1': 0.34; 'there': 0.34; 'quite': 0.34; "can't": 0.34; 'function.': 0.35; 'subject: ?': 0.35; 'skip:o 20': 0.36; 'some': 0.37; 'but': 0.37; 'subject:: ': 0.38; 'problem.': 0.38; 'should': 0.39; "there's": 0.39; 'to:addr:python.org': 0.39; "i'd": 0.40; 'received:172.20': 0.73; 'sender:addr:web.de': 0.84; 'subject:skip:o 10': 0.84; '-->': 0.91; 'something.': 0.91; 'faith': 0.96
Date Thu, 28 Jul 2011 23:06:22 +0200
From Alexander Kapps <alex.kapps@web.de>
User-Agent Mozilla/5.0 (X11; U; Linux i686; de-DE; rv:1.9.2.18) Gecko/20110617 Thunderbird/3.1.11
MIME-Version 1.0
To python-list@python.org
Subject Re: list comprehension to do os.path.split_all ?
References <1954d20a-7177-45d9-afa0-e98b171b2822@w27g2000yqk.googlegroups.com> <CALwzidkitibWKDExSxQ7xNVj3OxoB8iSgaavO=8x8q=nSB+_Xw@mail.gmail.com>
In-Reply-To <CALwzidkitibWKDExSxQ7xNVj3OxoB8iSgaavO=8x8q=nSB+_Xw@mail.gmail.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Sender alex.kapps@web.de
X-Sender Alex.Kapps@web.de
X-Provags-ID V01U2FsdGVkX194WmNkgyUeKiXBo/2eJbX2vz+wtRQqajeO0die cD0ocHlKB64Vhk4NTc34vk6LyLIPr9XgjQgKb5pDjDGMlUIM4r yFxjEeibc=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1591.1311887224.1164.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311887224 news.xs4all.nl 23937 [2001:888:2000:d::a6]:33573
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10479

Show key headers only | View raw


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']

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