Reputation: 391
I am using a simple python with
statement such as below to write to a log file.
with open(filename, 'a+') as f:
do_stuff1()
f.write('stuff1 complete. \n')
do_stuff2()
f.write('stuff2 complete. \n')
do_stuff3()
f.write('stuff3 complete. \n')
I am finding that my script fails intermittently at do_stuff2()
however in the log file I do not find the line "stuff1 complete" as I would expect if the file was closed correctly as should happen when using with
. The only reason I do know the script is failing at do_stuff2()
without my log working is because this function calls an API that does its own logging and that other log file tells me that 2 has been executed even if it did not complete.
My question is what sort of error would have to occur inside the with
statement that would not only stop execution but also prevent the file from being closed correctly?
Some additional information:
Obviously I am going to try to fix the problem itself but at this point my question is focused on what could happen inside the with
(potentially some number of layers below my code) that could break its intended functionality of closing the file properly regardless of whether the content of the with
is executed successfully.
Upvotes: 2
Views: 649
Reputation: 85512
The with
can only close the file if there is an exception. If there is a segfault inside an extension, no exception might be raised and the process dies without giving Python a chance to close the file. You can try to use f.flush()
at several places to force Python to write to the file.
Upvotes: 2