STR that should be file?

j2 spamfilter2 at mupp.net
Tue Mar 25 05:25:24 EST 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?


from ftplib import FTP
import sys
import ConfigParser
ftpsite = 'ftpeur.nai.com'
directory ='/pub/antivirus/datfiles/4.x/'
updateini = 'update.ini'
tempdir = 'c:\\naitemp\\'

# This will handle the data being downloaded
# It will be explained shortly
def handleDownload(block):
    file.write(block)
    print ".",

#COde to fetch a file, and write it to disk
def getFile(Path, Name, Destination):
    ftp = FTP(ftpsite)
    try:
        print ftp.login()
    except:
        print "Unable to log on to FTP"
        sys.exit(1)
    try:
        print 'Changing to ' + Path
        ftp.cwd(Path)
    except:
        print "Unable to CWD"
        sys.exit(5)
    #Try fetiching the file
    print "Saving file to " + tempdir + Destination
    file = open(tempdir + Destination, 'wb')
    print "Getting " + Name
    ftp.retrbinary('RETR ' + Path + Name , handleDownload)
    print 'Closing file ' + Name
    file.close()
    ftp.close()

getFile(directory,'update.ini','update.ini')


THis gives me

>>> 230 Anonymous access granted, restrictions apply.
Changing to /pub/antivirus/datfiles/4.x/
Saving file to c:\naitemp\update.ini
Getting update.ini
Traceback (most recent call last):
  File
"C:\Python22\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Python22\Projects\FTPNAI.py", line 76, in ?

  File "C:\Python22\Projects\FTPNAI.py", line 42, in getFile
    ftp.retrbinary('RETR ' + Path + Name, handleDownload)
  File "C:\Python22\lib\ftplib.py", line 390, in retrbinary
    callback(data)
  File "C:\Python22\Projects\FTPNAI.py", line 20, in handleDownload
    file.write(block)
TypeError: descriptor 'write' requires a 'file' object but received a 'str'
>>>






More information about the Python-list mailing list