ntpath bug or expected?

dsavitsk dsavitsk at e-coli.net
Wed Sep 11 21:18:21 EDT 2002


python 2.1.3.  this is pretty minor ... os.path.join() on win32 fails if the
first path component is C: (or any other letter).  that is, if the first
component is a drive letter, it must contain either '\\' or '/' or else the
path is wrong.  this makes it the only component which must have slashes.

example
>>> os.path.join('C:', 'path', 'to', 'file.txt')
C:path\\to\\file.txt

the offending code is below and can be easily fixed by removing the ':' from
'/\\:'

def join(a, *p):
    """Join two or more pathname components, inserting "\\" as needed"""
    path = a
    for b in p:
        if isabs(b):
            path = b
        elif path == '' or path[-1:] in '/\\:':
            path = path + b
        else:
            path = path + "\\" + b
    return path





More information about the Python-list mailing list