Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Write to next empty line

Tags:

python

I'm trying to write the output of something that is being done over three big iterations and each time I'm opening and closing the outfile. Counters get reset and things like this after the iterations and I'm a massive newb and would struggle to work around this with the shoddy code I've written. So even if it's slower I'd like change the way it is being output.

Currently for the output it's just rewriting over the first line so I have only the output of the last run of the program. (tau, output are variables given values in the iterations above in the code)

with open(fileName + '.autocorrelate', "w") as outfile:
    outfile.writelines('{0}     {1}{2}'.format(tau, output, '\n'))

I was wondering if there are any quick ways to get python to check for the first empty line when it opens a file and write the new line there?

like image 935
cc211 Avatar asked Dec 26 '22 23:12

cc211


1 Answers

Open with "a" instead of "w" will write at the end of the file. That's the way to not overwrite.

like image 189
f p Avatar answered Dec 29 '22 15:12

f p