python TypeError:dl()只需4个参数(给定3个)

Mao*_*ght 1 python class urllib

我得到一个类型错误,我的类在其函数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
Run Code Online (Sandbox Code Playgroud)

在运行时我得到了类型错误,但我在类函数dl中有自己,我做错了什么?

>>> 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)
>>>  
Run Code Online (Sandbox Code Playgroud)

Mar*_*cSB 5

您需要定义pck参数.

s.dl("C:\hh.html","http://stackoverflow.com/questions/82831/check-if-a-file-exists-using-python", True)
Run Code Online (Sandbox Code Playgroud)

或者如果您想使用默认值使参数可选,请定义如下方法:

def dl(self,_dir,_url,pck=True):
Run Code Online (Sandbox Code Playgroud)