Convert list to file object without creating an actual file.

Benjamin musiccomposition at gmail.com
Thu Jan 24 22:56:35 EST 2008


On Jan 24, 8:57 pm, Bart  Kastermans <bkast... at gmail.com> wrote:
> I have written a little program that takes as input a text file,
> converts
> it to a list with appropriate html coding (making it into a nice
> table).
> Finally I want to upload this list as a textfile using ftp.
>
> If homeworkhtml contains the list of lines;
> e.g. homeworkhtml = ["<table>", "<tr>", "<td>", "test", "</td>" .....
>
> I want to call:
> ftp.storlines("STOR " + filename, homeworkhtml)
>
> which gives me the error
> Traceback (most recent call last):
>   File "./testhw.py", line 67, in ?
>     ftp.storlines("STOR " + filename, homeworkhtml)
>   File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/
> python2.3/ftplib.py", line 428, in storlines
> AttributeError: 'list' object has no attribute 'readline'
Perhaps what you want is StringIO. It lets your pretend a string is a
file so ftplib won't choke. You'll have to convert your list to a
string, though (perhaps with join):
from cStringIO import StringIO
fake_file = StringIO("".join(my_list))
>
> Expected since homeworkhtml is in fact not a file.  Is there a way
> to convert this list to a file object without first writing it to disc
> and
> then opening the resulting file?
>
> Best,
> Bart




More information about the Python-list mailing list