By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,275 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,275 IT Pros & Developers. It's quick & easy.

Convert list to file object without creating an actual file.

P: n/a
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'

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
Jan 25 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jan 24, 8:57 pm, Bart Kastermans <bkast...@gmail.comwrote:
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
Jan 25 '08 #2

P: n/a
On Thu, 24 Jan 2008 18:57:58 -0800, Bart Kastermans wrote:
I have written a little program that takes as input a text file,
....
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?
The StringIO module is your friend, together with a couple of basic
Python techniques.

>>alist = ["<table>\n", " <tr>\n",
.... " <td>", "Nobody expects the Spanish Inquisition!",
.... "</td>\n", " </tr>\n", "</table>\n"]
>>print ''.join(alist) # but strings don't have a readlines method...
<table>
<tr>
<td>Nobody expects the Spanish Inquisition!</td>
</tr>
</table>
>>>
f = StringIO.StringIO()
f.writelines(alist)
f.getvalue()
'<table>\n <tr>\n <td>Nobody expects the Spanish Inquisition!</td>\n
</tr>\n</table>\n'
>>f.seek(0) # don't forget to reset the file pointer!
print f.read() # also has readlines
<table>
<tr>
<td>Nobody expects the Spanish Inquisition!</td>
</tr>
</table>


--
Steven
Jan 25 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.