Overwrite variable to file in python -
i defined function generates names , run loop:
output = open('/tmp/namegen-output.txt', 'w') while true: var = namegen(name) . . . if sth: output.write(var) elif other: output.write(var) break else: break output.close()
update:
first iteration ,content of namegen-output.txt
:
a b c
second iteration:
a b c d e
and etc
so if overwrite second iteration just:
d e
what going ask is:
as see var equals namegen()
, each iteration content of var written namegen-output.txt
want overwrite output of each iteration of namegen()
namegen-output.txt
no appending it.
could possibly me?
thank you
you can truncate existing file without opening , closing it, , flush ensure written to:
output = open('/tmp/namegen-output.txt', 'w') while true: var = namegen() . . . if not sth , not other: break else: output.flush() output.seek(0) output.truncate() output.write(var) output.flush() output.write(var) if other: break output.close()
Comments
Post a Comment