[python-win32] problem with UNICODE network paths and WNetAddConnection2

Shailesh Kumar shaileshk at gmail.com
Tue Aug 7 16:50:55 CEST 2007


Hi,
I am facing a problem with network paths which contain non-ascii
characters when trying to map them.

More precisely, the location contains Korean characters.
location = ur'\\baton1\TestStreams\shailesh\게댜ㅕㅔ걷;어니;ㅓ'

WNetAddConnection2 function expects string type as remoteName argument.

A simple call certainly fails:
win32wnet.WNetAddConnection2(
        win32netcon.RESOURCETYPE_DISK,
        None, location, None,
        None, None)
Traceback (most recent call last):
  File "D:\eclipseworkspaces\training\pytraining\win32networkstuff\unc.py",
line 66, in ?
    None, None)
UnicodeEncodeError: 'ascii' codec can't encode characters in position
30-44: ordinal not in range(128)


I couldn't figure out the right encoding for the UNICODE string. I tried both
'utf8' and 'mbcs' encodings, something like:

win32wnet.WNetAddConnection2(
        win32netcon.RESOURCETYPE_DISK,
        None, location.encode('utf8'), None,
        None, None)

Traceback (most recent call last):
  File "D:\eclipseworkspaces\training\pytraining\win32networkstuff\unc.py",
line 66, in ?
    None, None)
pywintypes.error: (53, 'WNetAddConnection2', 'The network path was not found.')


Sometimes 'Access is Denied' string is also reported.

What is the right way to handle this case?
Your help is highly appreciated.

I am running Python 2.4.4 on Win XP Pro with pywin32-209 revision.

With regards,
-Shailesh


More information about the python-win32 mailing list