質問

I'm writing a module that uses FTPLib to fetch files. I want to find a way to pass a value(in addition to the block) to the callback. Essentially, my callback is

 def handleDownload(block, fileToWrite):
    fileToWrite.write(block)

And I need to call

ftp.retrbinary('RETR somefile', handleDownload)

And have it pass a file handle. Is there a way to do this?

役に立ちましたか?

解決

You can close over the fileToWrite variable with a lambda:

fileToWrite = open("somefile", "wb")
ftp.retrbinary("RETR somefile", lambda block: handleDownload(block, fileToWrite))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top