Python - Python中的pythoncom.com_error处理3.2.2 [英] Python - pythoncom.com_error handling in Python 3.2.2

查看:717
本文介绍了Python - Python中的pythoncom.com_error处理3.2.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.2.2,并构建一个Tkinter接口来做一些Active Directory更新。我无法处理pythoncom.com_error异常。



我从这里获取了一些代码:
http://code.activestate.com/recipes/303345-create-an-account-in- ms-active-directory /



但是,我使用以下(直接从上述站点)处理引发的异常:

 除了pythoncom.com_error,(hr,msg,exc,arg):

此代码与许多我看到处理这些异常的站点一致,但是使用Python 3.2.2,如果在pythoncom.com_error后面加上逗号,则会收到语法错误。如果我删除逗号,程序启动,但是当异常被提出时,我得到其他异常,因为hr,msg等没有被定义为全局变量。



如果我删除逗号和括号中的所有位,那么这一切都会很好,除非我无法确切地看到异常中发生了什么,我想要的,所以我可以通过AD的实际错误消息



有没有人知道如何在Python 3.2.2中正确处理这些pythoncom异常?



提前感谢

解决方案

你只需要使用现代的 except-as 语法,我想:

  import pythoncom 
import win32com
import win32com.client

location ='fred'
try:
ad_obj = win32com.client.GetObject(location )
除了pythoncom.com_error作为错误:
print(error)
print(vars(error))
print(error.args)
hr,msg,exc,arg = error.args

其中

 ( -  2147221020,'无效语法',无,无)
{'excepinfo':无,'hresult':-2147221020,'strerror':'无效语法','argerror':无}
(-2147221020,'无效语法',无,无)

对我来说(尽管我从来不知道args顺序是否真的是它的外观喜欢,所以我可能会明确地提及密钥;别人可能肯定知道。]


I am using Python 3.2.2, and building a Tkinter interface to do some Active Directory updating. I am having trouble trying to handle pythoncom.com_error exceptions.

I grabbed some code from here: http://code.activestate.com/recipes/303345-create-an-account-in-ms-active-directory/

However, I use the following (straight from the above site) handle the exceptions raised:

except pythoncom.com_error,(hr,msg,exc,arg):

This code is consistent with many of the sites I have seen that handle these exceptions, however with Python 3.2.2, I get a syntax error if I include the comma after "pythoncom.com_error". If I remove the comma, the program starts, but then when the exception is raised, I get other exceptions because "hr", "msg" etc are not defined as global variables.

If I remove the comma and all of the bits in the brackets, then it all works well, except I can't see exactly what happens in the exception, which I want so I can pass through the actual error message from AD.

Does anyone know how to handle these pythoncom exceptions properly in Python 3.2.2?

Thanks in advance!

解决方案

You simply need to use the modern except-as syntax, I think:

import pythoncom
import win32com
import win32com.client

location = 'fred'
try:
    ad_obj=win32com.client.GetObject(location)
except pythoncom.com_error as error:
    print (error)
    print (vars(error))
    print (error.args)
    hr,msg,exc,arg = error.args

which produces

(-2147221020, 'Invalid syntax', None, None)
{'excepinfo': None, 'hresult': -2147221020, 'strerror': 'Invalid syntax', 'argerror': None}
(-2147221020, 'Invalid syntax', None, None)

for me [although I'm never sure whether the args order is really what it looks like, so I'd probably refer to the keys explicitly; someone else may know for sure.]

这篇关于Python - Python中的pythoncom.com_error处理3.2.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆