split a directory string into a list

Josef Meile jmeile at hotmail.com
Mon Feb 28 05:53:40 EST 2005


> The most obvious case where it wouldn't work would be for a UNC path name. 
> Using the string split method gives two empty strings:
> 
> 
>>>>os.path.normpath(r'\\machine\share').split(os.path.sep)
> 
> ['', '', 'machine', 'share']
> 
> 
> 
> whereas the splitpath function I proposed gives you:
> 
> 
>>>>splitpath(r'\\machine\share')
> 
> ['\\\\', 'machine', 'share']
> 
> So to find out the type of path (relative, absolute, unc), you only have to 
> consider the first element with my function but you have to look at the 
> first two elements if you just naively split the string.
Thanks for the explanation. I forgot that you could do thinks like that
on windows.

> 
> Also a relative windows path with a drive letter doesn't get fully split:
> 
> 
>>>>os.path.normpath(r'c:dir\file').split(os.path.sep)
> 
> ['c:dir', 'file']
> 
>>>>splitpath(r'c:dir\file')
> 
> ['c:', 'dir', 'file']
Again, I forgot it.

> If you really are worried about speed (and are sure you aren't optimising 
> prematurely), then you could combine some special case processing near the 
> start of the string with a simple split of the remainder.
No, I'm not worried about speed. Actually, the original post wasn't
mine. I was just curious about your answer.

Regards,
Josef



More information about the Python-list mailing list