Question

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?

Was it helpful?

Solution

You can close over the fileToWrite variable with a lambda:

fileToWrite = open("somefile", "wb")
ftp.retrbinary("RETR somefile", lambda block: handleDownload(block, fileToWrite))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top