【发布时间】:2016-08-08 07:31:35
【问题描述】:
我的代码发生了一些奇怪的事情,我的第一个函数是这样的:
def function1():
try : #1
#try to open a file
#read file
#return info variable from the file
except : #1
try : #2
#try to open a web page
#read web page
if directory1 not in directorylist :
#create directory1
#change working directory to directory1
else :
#change working directory to directory1
#write web page content in a file
#return info variable from the file
except : #2
try : #3
#try to open a second web page
#print error message 1
except : #3
#print error message 2
#set info variable to None
#return info variable
所以这个函数在主程序中调用时可以完美运行,但是当我尝试在另一个函数2中调用function1时,try#2和except#2都被执行了!原因 directory1 已创建并打印错误消息 1,我的 info 变量也等于 None。
如何在第二个函数混乱中调用 function1 和 except 子句?
谢谢!
【问题讨论】:
-
可能在那之后出现了错误。也许将页面内容写入文件。
-
@zondo 无论如何,被main调用时有效,被其他函数调用时无效
标签: python python-3.x exception error-handling