Convert list to file object without creating an actual file.

B

Bart Kastermans

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
 
B

Benjamin

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))
 
S

Steven D'Aprano

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",
.... " said:
print ''.join(alist) # but strings don't have a readlines method...
<table>
<tr>
<td>Nobody expects the Spanish Inquisition!</td>
</tr>
<table>
<tr>
<td>Nobody expects the Spanish Inquisition!</td>
</tr>
</table>
 
B

Bart Kastermans

The suggestion to use StringIO to make a string have the same
interface
as a file works perfect in my situation. Here is now the working
result
(just in case it is useful to anyone) part of this is still a definite
hack and suggestions for improvement are certainly appreciated.

Want:
have an easily maintainable file containing homework assignments, but
have it nicely formatted on my webpage.

Solution:
have a file with due dates and assignments as follows:

Mon Jan 28
1.1: 1,2,3

(at this point only the indentation is taken into account).

Then use the following script (setting some variables and server,
login, and passwd for the ftp connection):

#!/usr/bin/env python

homeworktxt = "homework.txt"
homeworkhtml = []

import sys
import ftplib
import StringIO

Indent = ' '
StartTable = '<table border="1" width="100%"><tr><td>Due</
td><td>Assignment</td></tr>\n<tr>'
EndTable = '</table>'
StartTitle = Indent + '<tr>\n' + Indent + Indent +'<td valign="top">'
EndTitle = Indent + Indent + '</td>\n' + Indent + Indent + '<td>'
BeforeItems = Indent + Indent + Indent + '<table>'
AfterItems = Indent + Indent + Indent + '</table>\n' + Indent +
Indent + '</td>\n' + Indent + '</tr>'
StartItem = Indent + Indent + Indent + Indent + '<tr><td>'
EndItem = Indent + Indent + Indent + Indent + '</td></tr>'


#if 1 >=len (sys.argv):
# print "Need an filename as an argument"
# print sys.argv[0] + " <filename>"
#else:

hw = open (homeworktxt).readlines()

Last = 0 # 0: at the start, 1: just printed an item, 2: just
printed a title

#print StartTable
homeworkhtml.append(StartTable)

for x in hw:
if ' ' == x[0]:
if 2 == Last:
homeworkhtml.append(BeforeItems)
homeworkhtml.append(StartItem)
homeworkhtml.append(Indent + Indent + Indent + Indent + Indent +
x.strip())
homeworkhtml.append(EndItem)
Last = 1
elif '#' != x[0]:
if 1 == Last:
homeworkhtml.append(AfterItems)
homeworkhtml.append(StartTitle)
homeworkhtml.append(Indent + Indent + Indent + x.strip())
homeworkhtml.append(EndTitle)
Last = 2
# else :
# homeworkhtml.append('COMMENT')
# homeworkhtm

if 1 == Last:
homeworkhtml.append(AfterItems)

homeworkhtml.append(EndTable)

for i in range(0,len(homeworkhtml)):
homeworkhtml = homeworkhtml + '\n'

homeworkhtmlfile = StringIO.StringIO()
homeworkhtmlfile.writelines(homeworkhtml)
homeworkhtmlfile.seek(0)

# open connection to the server
ftp = ftplib.FTP('server', 'login', 'password' )

# go to location of root of website on server
ftp.cwd('httpdocs')

# put the contends of a file
filename = "test.html"

ftp.storlines("STOR " + filename, homeworkhtmlfile)

# quite the connection
ftp.quit()



This will result in an html file that you can include (say with
php include) into a webpage displaying a table. Examples of the
result are currently on my webpage (www.bartk.nl/teach.php ) (if
you read this much later look on the waybackmachine).


Again, thanks for the suggestions,

Best,
Bart

PS: apologies for the mixing of spaces and tabs. I had a computer
crash and have not yet corrected the settings. So this is a
combination of default behavior and my preferred settings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top