Text File

Magnus L. Hetland mlh at vier.idi.ntnu.no
Wed Jan 12 18:32:27 EST 2000


Simon Faulkner <news at titanic.co.uk> writes:

> Hi All,
> 
> I would like to write a CGI program that diskplays a small (<1k) text
> file on a web page and saves any changes made by the browser back to
> the same file.
> 
> Any sugestions to help me get started?
> 
> Specifically
> 
> Which data type should I use and how do I read the text  file into
> it?

Just do:

  file = open("filename.txt")
  text = file.read()
  file.close()

And there it is - as a string. Put that into a text-area in your HTML
output for the user to edit. When you get the result, you'll probably
get it in a manner similar to this:

  data = FieldStorage()
  text = data["text"].value()

  file = open("filename.txt","w")
  file.write(text)
  file.close()

And there you are. Now all you need to do is build the CGI stuff and
HTML around it :)

> 
> Cheers  
> 
> 
> Simon
> 

--

  Magnus
  Lie
  Hetland                                mailto:<anything>@hetland.org



More information about the Python-list mailing list