list comprehension to do os.path.split_all ?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jul 29 20:55:52 EDT 2011


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




More information about the Python-list mailing list