Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a value to the ftp.retrbinary callback

Tags:

python

ftp

ftplib

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?

like image 249
Feasoron Avatar asked Aug 21 '12 17:08

Feasoron


1 Answers

You can close over the fileToWrite variable with a lambda:

fileToWrite = open("somefile", "wb")
ftp.retrbinary("RETR somefile", lambda block: handleDownload(block, fileToWrite))
like image 107
Frédéric Hamidi Avatar answered Oct 09 '22 03:10

Frédéric Hamidi