我收到类型错误,我的类在其函数 dl() 中使用了 self
import urllib
import httplib
import os.path
###SAI22 Library###
def exists(site):
conn = httplib.HTTPConnection(site)
conn.request("HEAD",site)
response = conn.getresponse()
conn.close()
return response.status == 200
class sai_download:
def dl(self,_dir,_url,pck):
if pck == True:
if exists(_url) == True:
urllib.urlretrieve(_url,_dir)
if os.path.isfile(_dir) == True:
print "Download successful"
return True
else:
print "Download failed"
return False
else:
print "Url isnt valid"
return False
elif pck == False:
if exists(_url) == True:
urllib.urlretrieve(_url,_dir)
return True
if os.path.isfile(_dir) == True:
return True
else:
return False
else:
return False
运行时出现类型错误,但类函数 dl 中有 self,我做错了什么?
>>> s = sai_download()
>>> s.dl("C:\hh.html","http://stackoverflow.com/questions/82831/check-if-a-file-exists-using-python")
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
s.dl("C:\hh.html","http://stackoverflow.com/questions/82831/check-if-a-file-exists-using-python")
TypeError: dl() takes exactly 4 arguments (3 given)
>>>
最佳答案
您需要定义 pck 参数。
s.dl("C:\hh.html","http://stackoverflow.com/questions/82831/check-if-a-file-exists-using-python", True)
或者如果你想使参数可选并具有默认值,请定义如下方法:
def dl(self,_dir,_url,pck=True):
关于 python 类型错误: dl() takes exactly 4 arguments (3 given),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27454691/