为什么这会失败? [英] Why does this fail?

查看:100
本文介绍了为什么这会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python新问题,为什么会失败?


谢谢,

Dave


- -testcase.py ---

import sys,urllib,htmllib

def Checkit(URL):

试试:

打印打开,URL

f = urllib.open(URL)

f.close()

返回1

除外:

返回0

rtfp = Checkit(" http://www.python.org/doc/Summary。 html")

if rtfp == 1:

print" OK"

else:

print" ;失败

python testcase.py

New to Python question, why does this fail?

Thanks,
Dave

---testcase.py---
import sys, urllib, htmllib
def Checkit(URL):
try:
print "Opening", URL
f = urllib.open(URL)
f.close()
return 1
except:
return 0

rtfp = Checkit("http://www.python.org/doc/Summary.html")
if rtfp == 1:
print "OK"
else:
print "Fail"
python testcase.py

推荐答案

在文章< vv ***** *******@corp.supernews.com>,

" Dave Murray" < DL ****** @ micro-net.com>写道:
In article <vv************@corp.supernews.com>,
"Dave Murray" <dl******@micro-net.com> wrote:
Python问题的新手,为什么会失败?

--- testcase.py ---
import sys,urllib,htmllib def Checkit(URL):
试试:
print" Opening",URL
f = urllib.open(URL)
f.close()
返回1
除了:
在这里,尝试

除了异常,详情

print" Exception:",详情返回0

rtfp = Checkit(" http://www.python.org/doc/Summary.html")
如果rtfp == 1:
打印OK
否则:
打印失败
New to Python question, why does this fail?

---testcase.py---
import sys, urllib, htmllib
def Checkit(URL):
try:
print "Opening", URL
f = urllib.open(URL)
f.close()
return 1
except: Here, try
except Exception, details
print "Exception:", details return 0

rtfp = Checkit("http://www.python.org/doc/Summary.html")
if rtfp == 1:
print "OK"
else:
print "Fail"




然后你会看到


打开 http://www.python.org/doc/Summary.html

例外:''模块''对象没有属性''打开''

失败

你可能意味着你的问题。


问候。 Mel。



Then you''ll see

Opening http://www.python.org/doc/Summary.html
Exception: ''module'' object has no attribute ''open''
Fail
You probably mean urlopen .

Regards. Mel.


在Sun,2004-01-04 18:58,Dave Murray写道:
On Sun, 2004-01-04 at 18:58, Dave Murray wrote:
Python新问题,为什么这样失败?


[...]尝试:
[...]除外:
New to Python question, why does this fail?
[...] try: [...] except:



[...]


因为你把所有的错误视为你所期望的。你的
应该在你的except子句中更具体。这样做你会看到

我的意思是:


试试:

无论

除了:

加注#raise发生什么异常

返回0


换句话说,你应该明确错误你沉默了。


此外,还不清楚Checkit()究竟应该做什么。是否应该验证URL确实存在?
?如果找不到404,则urllib不会引发

错误 - urllib2会发生错误。试试吧。


干杯,


// m


[...]

Because you''re treating all errors as if they''re what you expect. You
should be more specific in your except clause. Do this and you''ll see
what I mean:

try:
whatever
except:
raise # raise whatever exception occurred
return 0

In other words, you should be explicit about the errors you silence.

Also, it''s not clear what Checkit() is actually supposed to do. Is it
supposed to verify the URL actually exists? urllib doesn''t raise an
error for 404 not found--urllib2 does. Try that instead.

Cheers,

// m


> >>>> " Dave和QUOT; == Dave Murray< dl ****** @ micro-net.com>写道:


Dave> Python问题的新手,为什么会失败?谢谢,戴夫


戴夫> f = urllib.open(URL)

urllib没有开放功能。相反,它有一个名为

URLopener的构造函数,它使用这样的方法创建一个对象。所以相反,你有

来说


opener = urllib.URLopener()

f = opener(URL)


问候,

艾萨克。
>>>>> "Dave" == Dave Murray <dl******@micro-net.com> writes:

Dave> New to Python question, why does this fail? Thanks, Dave

Dave> f = urllib.open(URL)

urllib does not have an open function. Instead, it has a constructor called
URLopener, which creates an object with such a method. So instead, you have
to say

opener = urllib.URLopener()
f = opener(URL)

Regards,
Isaac.


这篇关于为什么这会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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