[python-win32] Try to got short path for files - but got error...

Mark Hammond mhammond at skippinet.com.au
Thu May 25 01:02:52 CEST 2006


I'm afraid your message isn't very clear.  You should try and copy the
smallest possible code that demonstrates your problem, exactly as Bobby did
in his reply.

>
> I got errors with unicode file names. See my unicode test file name:
>    UFN=u'%s\\xA\xff'%os.getcwd()

Is that what you mean?  You have a literal backslash, followed by 'xa',
followed by unicode char ff.  Eg:

>>> u'%s\\xA\xff'%os.getcwd()
u'c:\\src\\xA\xff'
>>>

The 'xA' is *not* a unicode char in your example.

> I want to get an universal code the convert (unicode) long path to short
> path.

There is no way you can do that.  Windows allocates the short name for a
file as it is created.  That short name is not guaranteed to be the same on
2 different machines.  The only way to get the short name is to ask Windows
for it.

> >>     Looks like you need a GetShortPathNameW() but it's not implemented.
> >>     Raise it as an issue on the pywin32 sourceforge bug register.
> >>     Tell Mark
> >>     I sent you :-)
> >>     Another thought: try using ctypes.

Note that Windows uses MBCS encoding - so you can use
win32api.GetShortPathName(filename).decode('mbcs') to get the unicode
filename.  That only works if the unicode characters are all in your current
codepage, but that is usually the case.  GetShortPathName does need
upgrading to accept a unicode object and in that case call
GetShortPathNameW.

Cheers,

Mark



More information about the Python-win32 mailing list