Skip to main content
 首页 » 编程设计

python之使用 'expect' 方法捕获 Python 异常

2025年05月04日7linjiqin

import sys 
try: 
        file = open("words.txt") 
expect(IOError): 
 
if file: 
    print "%s" % file 
else: 
    print "Cant the %s file" % "words.txt" 

这给了我一个错误 -

File "main.py", line 4  
    expect(IOError): 
    SyntaxError: invaild syntax 

我出了什么问题/你如何解决这个问题

请您参考如下方法:

实际上,它是异常的:

例如:

except IOError: 
    print "Error opening file!"