newbie win32 stuff

John Nielsen nielsenjf at my-deja.com
Mon Dec 27 18:53:19 EST 1999


I recently switched to python for win32 stuff from using C++ and Perl
and have found it to be very easy to use.

The try: except: block catches 'exceptions' which are basically errors
that typically cause the program to halt. In other words, it gives you
the ability to decide what the program should do when faced with an
error. Perhaps you don't want it to exit or maybe you need to do some
cleanup before exiting.

Another useful thing to do with the 'except:' part of the block is print
out the actual error.

For example,
try:
   . . . bad code here  . . .
except:
   print 'I got an error and it is',sys.exc_type , sys.exc_value

If you import sys, you can use that module to show you the error python
got.

I tried some some registry code in conjunction with NetServerEnum in
pythonwin and had no problems. Where is your code hanging?

john



In article <TlR94.2978$k7.129979 at news1.teleport.com>,
  "tony roth" <roth at teleport.com> wrote:
> I have a script that when I run it within pythonwin it behaves poorly
but
> when I run it within idle it runs just fine.  Below is the code, I'm
just
> learning python so forgive me if it looks screwy.   Under idle it
creates a
> tuple "dm" which contains the names of all workstations within the
> "xyzdomain" and then enumerates the nameserver values but under
pythonwin it
> just hangs the process.  Any ideas.
> thanks
>
> ps the try/except construct is still confusing to me.  Is the try
catching
> all exceptions generated beween the the try: except: construct?
>
> import win32net,win32netcon,win32api,win32con
>
dm=win32net.NetServerEnum('',100,win32netcon.SV_TYPE_WORKSTATION,'xyzdom
ain'
> ,0,4096*2)
> for x in dm[0]:
>    try:
>
>
rk=win32api.RegConnectRegistry(str(x['name']),win32con.HKEY_LOCAL_MACHIN
E)
>
>
nk=win32api.RegOpenKeyExrk,'SYSTEM\\CurrentControlSet\\Services\\NetBT\\
Adap
> ters\\',0,win32con.KEY_ALL_ACCESS)
>       test=win32api.RegEnumKey(nk,0)
>       regkey='SYSTEM\\CurrentControlSet\\Services\\NetBT\\Adapters\\'
+ test
>       nk=win32api.RegOpenKeyEx(rk,regkey,0,win32con.KEY_ALL_ACCESS)
>       ns=win32api.RegQueryValueEx(nk,'NameServer')
>       print str(x['name']),
>       print ns[0][0],
> except:
>       print "can not contact " + str(x['name'])
>
>

--
nielsenjf at my-Deja.com


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list