ftp.storbinary与ftp.retrbinary不一样吗?

时间:2014-01-30 20:28:27

标签: python ftp

我正在尝试下载并上传到FTP服务器。下载工作正常,但上传正在吐出这个错误:

错误:AttributeError(“'function'对象没有属性'read'”,)

def download_handle(block):
    global sizeWritten
    dFile.write(block)
    os.system('CLS')
    sizeWritten += len(block)
    percentComplete = sizeWritten / totalSize
    percentComplete = round((percentComplete*100),1)

    print (percentComplete, "% complete")

try:
    dFile = open('filename', "wb")
    print("DL File opened")
    ftp.retrbinary("RETR " + filename, download_handle)
    print("Download Completed")
    dFile.close()
except Exception as e:
    print("Error:", repr(e))

def upload_handle(block):
    global sizeWritten
    upFile.write(block)
    os.system('CLS')
    percentComplete = sizeWritten / totalSize
    percentComplete = round((percentComplete*100),1)

    print (percentComplete, "% complete")


try:
    upFile = open('filename', "rb")
    print("UL File opened")
    ftp.storbinary("STOR " + filename, upload_handle)
    print ("Upload completed")
    upFile.close()
except Exception as e:
    print("Error:", repr(e))

1 个答案:

答案 0 :(得分:0)

请注意,retrbinary方法要求您传递将用于写入数据的函数。通常你应该通过dFile.write。我不知道download_handle是什么,因为你没有定义它,但它可能不是正确的方法。同样,对于storbinary,您要传递upFile.read