list comprehension to do os.path.split_all ?

Alexander Kapps alex.kapps at web.de
Fri Jul 29 16:22:54 EDT 2011


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?



More information about the Python-list mailing list