[Tutor] Append To File

sasson iris sassonir@cs.bgu.ac.il
Sun, 25 Feb 2001 00:03:52 +0200 (IST)


Hi
just write f.write(output +"\n")
On Sat, 24 Feb 2001, Jason Drake wrote:

> Okay, first I will admit that I'm still digesting Danny Yoo's advice re: string interpolation. I will make attempts to use the technique he outlined some time today. What I'm up against right now has to do with appending to a file. My current code reads as follows:
> 
> #!/usr/bin/python
> # append.cgi
> # Author: Frik -- www.lowerstandard.com
> # append data to a file, then print the data to screen
> #
> 
> output = "no"
> 
> print "Content-type: text/html\n"
> 
> ###
> file = open('testdb.txt', 'w')
> file.write(output)
> ###
> print output
> ###
> 
> Although simple, this does exactly what I want it to do at this stage except for one little detail. If I hit this cgi it will write 'no' to my text file as it is supposed to, but I want to write to a new line rather than onthe same line directly after the first. At present my text file which is being appended to reads
> 
> nonononono
> 
> What I want it to read as is
> 
> no
> no
> no
> no
> no
> 
> The obvious question, how do I write to a new line in an opened file?
> 
> TIA
> 
> Jay D
>