list comprehension to do os.path.split_all ?

Hrvoje Niksic hniksic at xemacs.org
Sun Jul 31 02:42:27 EDT 2011


Neil Cerutti <neilc at norwich.edu> writes:

> On 2011-07-29, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>> 	Fine... So normpath it first...
>>
>>>>> os.path.normpath(r'C:/windows').split(os.sep)
>> ['C:', 'windows']

That apparently doesn't distinguish between r'C:\windows' and
r'C:windows'.  On Windows the first is an absolute path, the second a
relative path, and both contain a drive letter.

>     while tail != '':
>         retval.append(tail)
>         head, tail = os.path.split(head)
>     else:
>         if os.path.isabs(path):
>             retval.append(os.path.sep)
>         return list(reversed(retval))

Note that using 'else' after 'while' is superfluous if the loop doesn't
contain a 'break' statement.



More information about the Python-list mailing list