STR that should be file?
aleax at aleax.it
Tue Mar 25 13:12:46 CET 2003
> Ok, as my code clearly demonstrates, i am a blatant newbie.. but i cant
> figure this out. Traceback at the bottom (Python 2.2 on win32) Help?
> # This will handle the data being downloaded
> # It will be explained shortly
> def handleDownload(block):
> print ".",
file is the name of a built-in type (the type of file objects). So,
file.write is an unbound method of the type, which needs to be called
with an instance of the type as its first argument -- and the first
argument you're using is 'block', a string, whence the traceback.
You're using name 'file' for a local variable in the following
function getFile (don't do that -- it's NOT a good idea to name
your own variables with names of builtin types!), but that does
not in the least affect function handleDownload.
Since the callback you want to use must have both state (a file
object) AND behavior (code), a simple function is not suitable:
you need either a closure or the instance of a class. With a
or with a class:
def __init__(self, somefile):
self.somefile = somefile
def __call__(self, block):
and in the function getFile to use either of these approaches
you need one small change, to wit:
> #COde to fetch a file, and write it to disk
> def getFile(Path, Name, Destination):
> ftp = FTP(ftpsite)
> print ftp.login()
> print "Unable to log on to FTP"
> print 'Changing to ' + Path
> print "Unable to CWD"
> #Try fetiching the file
> print "Saving file to " + tempdir + Destination
> file = open(tempdir + Destination, 'wb')
change this to, e.g.,
myfile = open(tempdir + Destination, 'wb')
or whatever, but don't use name 'file' -- just advice!
> print "Getting " + Name
insert here, either:
handleDownload = makeDownloader(myfile)
handleDownload = Downloader(myfile)
> ftp.retrbinary('RETR ' + Path + Name , handleDownload)
> print 'Closing file ' + Name
this needs to become:
to match the variable name change above suggested.
More information about the Python-list