mimicking a file in memory

mgierdal at gmail.com mgierdal at gmail.com
Wed Dec 12 13:41:57 EST 2007


On Nov 20, 4:37 pm, Jarek Zgoda <jzg... at o2.usun.pl> wrote:

> Try with StringIO/cStringIO, these modules are supposed to give you
> in-memoryobjects compatible with file object interface.

I found this solution not working.
I had similar problem: I wanted to write some string into the in-
memory file, then transfer it via ftp to some file and forget in-
memory  content.

from ftplib import FTP
ftp = FTP('ftp.server.org')
ftp.login('ID','pswd')
import StringIO
filename = 'some_file.txt'
command = 'STOR ' + filename
outfile = StringIO.StringIO()
outfile.write(some_string + '\n')
ftp.storlines(command, outfile)
ftp.quit()
outfile.close()

The file shows up on the FTP server, but with ZERO length. I think the
problem is that ftp.storelines attempts to use outfile's read()
function, which is not present in StringIO objects (they use
getvalue() instead). Quite an annoying inconsistency.
Any thoughts, please?

--
Marcin



More information about the Python-list mailing list