Trying to understand how the
FTP.storbinary(command, file[, blocksize, callback, rest])
The callback function is called for
each block of data received, with a
single string argument giving the data
an integer is required
If your callback is
def handle(block): f.write(block) print ".",
Python has first class functions that can be passed as params- this is the point of a callback- you you pass the function as param to the storbinary call-
ftp.storbinary(command="stor someFileNameOnServer", file=open("localFile",'rb'), callback=handle,blocksize=1024)
From the python doc,
callback is an optional single parameter callable that is called on each block of data after it is sent.
It's purely a post-processing method for e.g. showing transfer status, it's called after each block of data is sent. Above it would be called after sending every 1024 bytes of data.
To implement transfer status, something like this-
sizeWritten = 0 totalSize = someMethodToGetTotalBytesInFile() def handle(block): global sizeWritten sizeWritten += 1024 percentComplete = sizeWritten / totalSize print "%s percent complete" %str(sizeWritten / totalSize)
os.path.getsize will give you the total size in bytes of your file.