Newbie: Problems with File IO

Flavian Hardcastle deathtospam43423 at altavista.com
Mon Feb 18 05:45:20 EST 2002


I cracked it! 

It was a lot simpler than I thought. Immensely obvious infact! 

In the part of the program which processes the fieldstorage, I inserted the 
lines...


The_Form = cgi.FieldStorage()

if (The_Form.has_key("handle")):
    user = str(The_Form["handle"].value)
elif not (The_Form.has_key("handle")):
    user = " "

So the user gets to decide their handle, and handle will be stored in  the 
string "user".

Then, in the actual html form itself, I inserted ....

print '<input type=text NAME="handle" SIZE="30" MAXLENGTH="30" value = 
"'+user+'">'

So whatever the user puts in the  input box "handle", will come back to 
them when the script re creates the html page.

Simplicity itself heh? I can't imagine why I didn't spot it before! Perhaps 
I did, but for some strange reason I thought the script would confuse 
everyone's handles.

I've already had a chat buddy test it out with me, it works ok. The only 
problem is that the posts are stored in the file "log.txt", and the script 
reproduces the file in it's entirety every chat session. The result being, 
the longer you chat, the bigger and bigger web page gets, until it's a cow 
to download.

For my next feat of mental contortion, I have to figure out a way to limit 
the number of posts in the "log.txt" file that are displayed. Ideally, I 
want the user to be able to specify that amount, so that (s)he can chose 
how many old posts (s)he wishes to view.


"Shaun Laughey" <shaun at laughey.com> wrote in 
news:mailman.1013981489.9375.python-list at python.org:

> If you want to remember them forever (sic) then use cookies.
> http://www.python.org/doc/current/lib/module-Cookie.html
> 
> If you want to remember them from post to post during the same "session"
> then use a session object (a temporary cookie in effect) or encode a
> unique session into the url (not as easy).
> 
> pyweblib.session contains all the functions you need.
> 
> Then you would simply
> 
> User connects
> Do they have a session?
> No - generate one.
> Do they have a username?
> No - ask for it and store in session username
> and
> Call the clean up function periodically to clear out old sessions.
> On your page display the session username.
> 
> Remember to use try: or has_key or else you will get errors if there is
> no username key in the session object.
> 
> For pyweblib see http://www.stroeder.com/pylib/PyWebLib/
> 
> Documentation isn't a strong point but the library should give you a
> help.
> 
> 
> Hope this helps
> Shaun Laughey,
> 
> -----Original Message-----
> From: python-list-admin at python.org [mailto:python-list-admin at python.org]
> On Behalf Of Flavian Hardcastle
> Sent: 17 February 2002 02:25
> To: python-list at python.org
> Subject: Re: Newbie: Problems with File IO
> 
> Chris Liechti <cliechti at gmx.net> wrote in 
> news:Xns91B81F8828230cliechtigmxnet at 62.2.16.82:
> 
>> Flavian Hardcastle <deathtospam43423 at altavista.com> wrote in 
>> news:Xns91B873A49D077AusYourStandingInIt at 210.49.20.254: 
>> 
>>> out_file = open("log.txt", "w")
>> mode 'a' does append (thats the one you want), 'w' starts a new file 
>> for writing, adding a plus 'a+' or 'w+' opens for read/write
>> 
> 
> Thanx! That's got me a bit further. What part of the standard Python
> manual 
> (the html one you get at Python.org) would that be under?
> 
> Also, if you could give me a hint as to how I might get the script to 
> remember people's usernames everytime they post, that'd be good.
> 



-- 
"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