I have to ensure that every minute I open, write, and close a log file. It must be closed as we trigger power loss randomly on the Rpi3. I known it is bad and I also know that you want to use something like this for a nice shutdown: https://www.raspberrypi.org/forums/viewtopic.php?t=39059
But, well I have no time to implement the hardware and on top of that the behavior is abnormal from my point of view as if I close(myfile) in Python then the system should release all handles, am I correct ? Hence any damage on power-loss should be randomly happening in the file system, if any, but not specifically to my file.... - which after a power cut ends up usually with "^@^@^@^@^@^@^@^@" - and needs to be cleaned...which triggers all sort os issues.
While testing my loop I cut power at 20 seconds, meaning 20 seconds AFTER the close(myfile) and 40 seconds before the next open(myfile,'a') & write(mytring) and close(myfile). - all this in a separate function from main which is called every minute. But the file EOF is anyway corrupted with many non UTF-8 characters.... The side effect for my application which needs to read the last data in the log file is that of course when I restart my program and read this file I have an issue - either to clean or re-initialise the log, which is bad for my work as the log can be huge and I have near RT data to collect....
My question is how to ensure Python (or the system?) release ALL handles on files it as used after a close() ?
For info, I have tried 3 different methods to open write and close on a log (text) and all end-up the same way of having bad EOF on power loss....- so suddenly I wonder if the issue is not somewhere else... as reference, I tried with (a)python logging function and (b) command-line push from python and (c) standard python open()/write()/close() and the very same issue of non UTF-8 characters/ corrupted EOF appears if I trigger a power-cut during my loop when the file should firmly closed and my program not writing to the log file.... Weird, Am I missing something?