当我试图运行这个简单的python脚本时,出现了一个错误:
for url in urllist:
try:
file = urllib2.urlopen (url)
urlstring = file.read ()
except:
print "Can't open URL"
pass
m = [ ]
m = re.findall (
r'file:///\\results.cal.ci.spirentcom.com\\smt\\SCMSmartTest\\\d+.\d+\\BLL\d+_IL\d+\\.*?\\TC\S+tcl',
urlstring)
copyFileCounts = 1
显示以下错误:
Traceback (most recent call last): File "D:\Python\untitled\regression.py", line 75, in urlstring) NameError: name 'urlstring' is not defined
不确定是不是打字错误,但是当你给
urlstring
和file
赋值时,会有一个空格。在当使用
try .. catch
时,您应该将try
块中的代码保持尽可能小:如果出现问题,您将知道原因,而无需进行大量调试。在您可以使用
else
子句。在所以你的代码应该是这样的:
在捕捉异常时还应该更具体,捕捉所有异常并不是一个好主意。在
使用另一个答案来修复代码:
做这件事并请求原谅是一个很好的做法,所以做任何你想做的事,然后抓住错误
相关问题 更多 >
编程相关推荐