list comprehension to do os.path.split_all ?

Michael Poeltl michael.poeltl at univie.ac.at
Sun Jul 31 07:40:30 CEST 2011


* Michael Torrie <torriem at gmail.com> [2011-07-31 03:44]:
> On Jul 29, 2011 6:33 PM, "Michael Poeltl" <michael.poeltl at 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




More information about the Python-list mailing list