ftp a list to a remote server

Chris Gonnerman chris.gonnerman at usa.net
Mon Mar 5 08:56:15 EST 2001


It depends on what exactly you mean by saving a list.  If your list contains
only strings (i.e. as from readlines()) and you want to write it as a text
file,
you might do this:  (this code is 1.5.2 + compatible)

import ftplib, StringIO, string

virtfile = StringIO.StringIO(string.join(mylist, ''))
conn = ftplib.FTP("my.host.com", "username", "password")
conn.storbinary("STOR " + myfilename, virtfile, 1024)
conn.close()

Note that newline termination is expected.  If you don't have newlines at
the
ends of your strings:

virtfile = StringIO.StringIO(string.join(mylist + [ "" ], '\n'))

On the other hand, you might want to pickle your data to the remote file.
Create
virtfile like this:

import ftplib, StringIO, string, pickle

virtfile = StringIO.StringIO('')
pickle.dump(mylist, virtfile)
virtfile.seek(0)

then proceed as above.  Of course for performance you could also use
cStringIO.

----- Original Message -----
From: "Austin Wilson" <austinw at bhpvillage.com>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Monday, March 05, 2001 2:07 AM
Subject: ftp a list to a remote server


> Hi
>
> I would like to save a list as a text file on a remote server using ftp.
>
> Is this possible? If so, could you please provide some sample code.
>
> Or do I have to save the file locally first then ftp that file to the
remote
> server?
>
> Thanks
> Austin
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list