Coding problems with ftplib and uploading a file

Fredrik Lundh effbot at telia.com
Fri Feb 18 16:49:49 EST 2000


pem at my-deja.com wrote:
> I am new to python and have my first program near completion, however
> the last step has caused me great troubles.  What I need to do is
> upload a file to a server via ftp.  Very simple.  My code looks like
> this:
>
>      ftp = FTP('servername')
>      ftp.login('uname', 'passwd')
>      ftp.sendcmd('cdup')
>      ftp.sendcmd('cdup')
>      ftp.cwd('/web')
>      fini = open('/myfile.html').readlines()
>      ftp.storlines('STOR ' fini)

"storlines" takes a file object, not a list of lines.  try
this one instead:

        file = open("/myfile.html")
        ftp.storlines("STOR myfile.html", file)

> All of the commands work just fine except for the storlines command.  I
> have read all of the documentation available to me at work (Programming
> in Python and Learning Python, and web docs on ftplib) but none are
> clear about the syntax of the storlines command.

hey, you should have looked in the eff-bot guide ;-)

btw, the library reference says:

    storlines (command, file)
    ...
    Store a file in ASCII transfer mode. "command" should
    be an appropriate "STOR" command (see storbinary()).
    Lines are read until EOF from the open file object "file"
    using its readline() method to privide the data to be
    stored.
    ...

which does explain how it works, although you might have
to read it a couple of times to sort it all out ("privide"? ;-)

</F>





More information about the Python-list mailing list