I have a python script that runs once a minute. For debugging, I want to create text files with errors/exceptions in case they occur. In case errors are absent, I don't want any log files.
EDIT: I can't wrap all my code in a try/except loop, so this doesn't help.
First, I am saving stderr to .txt doing:
sys.stderr = open('ERRORS_%s.txt' % (infile), 'a')
with open("%s_Error_Messages.txt" % (MSKSN), "a") as myfile:
myfile.close()
Second, I then try to delete empty "%s_Error_Messages.txt" % (MSKSN) files before any raise SystemExit positions since no error was raised (otherwise, the raise SystemExit position would not have been reached).
if os.path.getsize('ERRORS_%s.txt' % (infile)) == 0:
os.remove('ERRORS_%s.txt' % (infile))
What I get is WindowsError: [Error 32] The process cannot access the file because it is being used by another process. I assume that this is because stderr is always in use by python.exe as long as the script runs.
For the first step, I also tried:
if sys.stderr != 0:
sys.stderr = open('ERRORS_%s.txt' % (infile), 'a')
Which does not work since sys.stderr is never 0.
How to write sys.stderr to .txt only in case of actual errors?