Tuesday, 23 January 2018

Writing to file using Python

I have a file called output.txt, which I want to write into from a few functions around the code, some of which are recursive.
Problem is, every time I write I need to open the file again and again and then everything I wrote before is deleted.
I am quite sure there is a solution, didn't find it in all questions asked here before..



def CutTable(Table, index_to_cut, atts, previousSv, allOfPrevSv):
print ('here we have:')

print atts
print index_to_cut
print Table[2]
tableColumn=0
beenHere = False
for key in atts:
with open("output.txt", "w") as f:
f.write(key)



and from another function:



def EntForAttribute(possibles,yesArr):
svs = dict()
for key in possibles:
svs[key]=(yesArr[key]/possibles[key])
for key in possibles:
with open("output.txt", "w") as f:
f.write(key)



All output I have is the last one written in one of the functions..

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...