Oct-21-2019, 11:28 AM
(Oct-18-2019, 01:39 PM)perfringo Wrote:(Oct-18-2019, 01:20 PM)kryptomatrix Wrote: Is it guaranteed, that "out.txt" will contain "abc"
Only taxes and death are guaranteed, everything else is optional .
In more serious note - this 'guarantee' can't be given as there might be another processes which can overwrite the file. Yes, 'abc' will be written into file but this is not the same that 'guaranteed to contain' in some point after the writing.
(Oct-18-2019, 11:44 PM)snippsat Wrote:Thank you. The standard is phrased a bit ambiguously.(Oct-18-2019, 11:21 PM)kryptomatrix Wrote: The question is, whether it is guaranteed to write when exit() is called (or earlier).No it's not guaranteed at all,it will fail in many cases as eg trying running in editors and not command line.
If i test in my editors will not write to file,dot not matter if i use exit() or close down the editor.
As long as the file object is open and not used it can be garbage collected.
To be sure has to useclose()
.
f = open("out.txt", "w") f.write("abc\n") f.close()Or better.
with open("out.txt", "w") as f: f.write("abc\n")