[Tutor] python win32 drive mapping help

wesley chun wescpy at gmail.com
Sat Sep 26 02:26:32 CEST 2009

>> I tried this:
>> win32net.NetUseDel(None, 1,{'local':'k:'})
>>  but I got this error:
>> Traceback (most recent call last):
>>  File "test.py", line 33, in <module>
>>    win32net.NetUseDel(None, 1,{'local':'k:'})
>> TypeError: an integer is required
> You have the wrong sequence for NetUseDel().  Try:
> win32net.NetUseDel(None, 'k:', win32net.USE_FORCE)
> (It's the 3rd argument it was complaining about, since that's a constant (of
> value 1, actually)).
> I'm guessing you're confusing the docs for NetUseAdd for NetUseDel.

dave is correct. you're assuming that the signatures of
NetUse{Add,Del}() are the same, and they're not. i actually never use
the 3rd arg and just make the call using 2 args:

>>> win32net.NetUseDel(None, r'Z:')

seems to work fine. i also read somewhere that win32net is the older
16-bit backwards-compatible way of doing things and that you should
use the stuff in win32wnet instead -- i wonder if this is related to
the OP error msg saying that win32wnet wasn't found... perhaps
win32net just wraps win32wnet?

anyway, here's how to do it using the win32wnet equivalents:

>>> import win32wnet
>>> win32wnet.WNetAddConnection2(1, 'Z:', r'\\remote\disk', None, 'user', 'secret')
>>> os.listdir(r'Z:\tmp')
['foo.txt', 'bar.txt', 'subdir']
>>> win32wnet.WNetCancelConnection2('Z:', 0, False)
>>> os.listdir(r'Z:\tmp')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
WindowsError: [Error 3] The system cannot find the path specified: 'Z:\\tmp/*.*'

i also read online that NetUseAdd() cannot take a user arg... only
password, so if the remote disk requires a username, then you must use
the win32wnet interface. also note that the force param is required
for WNetCancelConnection2(), so i pass in False for that (as opposed
to getting the full constant from win32netcon).

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009

"Python Web Development with Django", Addison Wesley, (c) 2009

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca

More information about the Tutor mailing list