win32api -- long vs short filenames

David Niergarth jdnier at
Tue Aug 10 00:10:46 CEST 1999

David Niergarth <jdnier at> wrote in message
news:7oa0n0$72n at
> I'm trying to expand a mangled DOS 8+3 name (from WinNT4.0) into the long
> version of the name. There's a win32api function for doing the opposite,
> example,
> >>> win32api.GetShortPathName(r'c:\long_path_name\long_file_name.txt')
> 'c:\\LONG_P~1\\LONG_F~1.TXT'
> But I can't figure out how to expand a mangled name into the original long
> name. Any ideas?
> --David Niergarth

David Asher (thanks, by the way) pointed me to win32api.FindFiles(), as in

>>> win32api.FindFiles(r'c:\PROGRA~1')[0][-2]
'Program Files'

This returns the long version of the last component of the path that you
feed it but not the long version of the entire path, which is what I'm
after. In case anybody is interested, here's a function that will return the
long version of the entire path from either a mangled absolute path or a
mangled simple file name in Python's current directory.

def win32_GetLongPathName(pathname):
    """Transform a mangled 8+3 pathname to original long name.

    pathname -- Can be an absolute path or a simple file or direcotry name
                if it exists in Python's notion of the current directory.

    If a simple file or directory name cannot be resolved or an absolute
    path cannot be found, the original pathname will be silently returned.

    import os, win32api
    if os.path.isabs(pathname):
        abspath = pathname
        cur_dir = os.getcwd()
        newpath = os.path.join(cur_dir, pathname)
        if os.path.isfile(newpath):
            abspath = newpath
            return pathname  # could not resolve relative path
    pieces = []
    head, tail = os.path.split(abspath)
    while tail:
            longtail = win32api.FindFiles(os.path.join(head, tail))[0][-2]
        except IndexError:
            return pathname  # could not find path
        pieces.insert(0, longtail)
        head, tail = os.path.split(head)
    pieces.insert(0, head)
    return apply(os.path.join, pieces)

Seems like there should be an easier way, but this works for me.

--David Niergarth

More information about the Python-list mailing list