Newbie: Problems with File IO

Flavian Hardcastle deathtospam43423 at altavista.com
Sat Feb 16 19:52:46 EST 2002


Why idn't it working?

I'm trying to write my own crude, html based chat script. I figure the way 
to do it is to use a Python cgi script to take info from a web form, store 
it in a separate text file, and then get the script to redisplay it in the 
same page the form was on. 

I re read the chapter on File IO in Josh Cagliati's beginner's tutorial 
http://www.honors.montana.edu/~jjc/easytut/easytut/ 

I'm trying to use the commands detailed there to store the cgi info in the 
file, but there's a problem. Everytime someone accesses the form, and the 
script burns the info into the text file, the info that was previously in 
the file gets erased. That's no good! That means that visitors to this chat 
site will only ever be able to see one post at a time - their own!

Can someone tell me what I might be doing wrong? What do I need to learn in 
order to fix the problem?

Here follows the script. The part I think I might be getting wrong is the 
part labelled ## The Action......

#! C:/Python22/python
import cgi, string
from time import time, ctime

print "Content-Type: text/html\n\n"
## This determines the type of output the cgi gives. Can be 'text/plain'

def TOPpage():
    print '<html>'
    print '<title>hairy fat bastard chat</title>'
    print '<body>'
    print '<br>'

def BOTpage():
    print"</body>"
    print"</html>"
    
TOPpage()

##The Action
The_Form = cgi.FieldStorage()
out_file = open("log.txt", "w")
for loop in The_Form.keys():
    out_file.writelines(The_Form[loop].value + "<BR>")
out_file.close()

in_file = open("log.txt","r")
chatters = in_file.read()

print chatters


print '<FORM method=post action="fieldtest1.py">'

print '<b>handle:</b>'
print '<INPUT type=text NAME="name" SIZE="30" MAXLENGTH="30">'
print '<br><b>chat:</b>'
print '<br>'
print '<TEXTAREA name = "chat" ROWS=10 COLS=80 SIZE="30" MAXLENGTH="30">'
print '</textarea>'
print '<br>'
print '<INPUT TYPE=submit VALUE="Get/Send"> </form> </center>'

BOTpage()



-- 
"You can tell whether a man is clever by his answers. You can tell whether 
a man is wise by his questions."  Naguib Mahfouz

netvegetable at dingoblue.net.au



More information about the Python-list mailing list