faqts : Computers : Programming : Languages : Python : Snippets : Network Tools

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

11 of 11 people (100%) answered Yes
Recently 8 of 8 people (100%) answered Yes

Entry

Uploading a binary file with ftplib

Jul 5th, 2000 10:03
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 388, Max M Rasmussen


"""
Packages: networking.internet
"""
"""
> I've submitted this to python.faqts.com, but no one has answered.  So
> I'm asking it here. . .
> 
> I'm trying to upload a binary file to a site using ftplib.  I can log in
> successfully and do "cd dir_name", but cannot figure out how to upload
> the file.  The standard Python module documentation seems a bit cryptic
> to me in this case (though it is usually quite good).  This is the
> command I have used:
> 
> ftp.storbinary('stor ravine.pyc',open('ravine.pyc','r').read(),2)
> 
> And this is my result:
> 
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
>   File "/usr/lib/python1.5/ftplib.py", line 366, in storbinary
>     buf = fp.read(blocksize)
> AttributeError: 'string' object has no attribute 'read'
> 
> Anyone have any insight?
Maybe this function can be of some help?
"""
###########
from ftplib import FTP
def FTP_GLOB_transfer(URL, TargetDir, UserName, Password,
GlobExpression):
    ftp = FTP(URL)   # connect to host, default port
    ftp.login(UserName,Password)
    ftp.cwd(TargetDir)
    FileTransferList = glob.glob(GlobExpression)
    print URL, TargetDir, UserName, Password, GlobExpression
    for file in FileTransferList:
        FileName = file[-8:]
        print file
        TheFile = open(file, 'r')
        ftp.storbinary('STOR ' + FileName, TheFile, 1024)
        TheFile.close()
    ftp.quit()
    ftp = None