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


Groups > comp.lang.python > #10473

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From Alan Meyer <ameyer2@yahoo.com>
Newsgroups comp.lang.python
Subject Re: list comprehension to do os.path.split_all ?
Date Thu, 28 Jul 2011 16:40:57 -0400
Organization A noiseless patient Spider
Lines 23
Message-ID <4E31C959.9050606@yahoo.com> (permalink)
References <1954d20a-7177-45d9-afa0-e98b171b2822@w27g2000yqk.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Info mx04.eternal-september.org; posting-host="cXfoUdE2tOmL54C/tFXK/Q"; logging-data="12109"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/u2Obgi7OaFLcGpwu+I58F"
User-Agent Mozilla/5.0 (Windows NT 6.0; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0
In-Reply-To <1954d20a-7177-45d9-afa0-e98b171b2822@w27g2000yqk.googlegroups.com>
Cancel-Lock sha1:J2SAQQiFZ95Z0RLWy0DawvXJAtE=
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10473

Show key headers only | View raw


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

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