Minimal example:
def main():
    for i in range(100):
        print("One line every 2s", end = "\n")
        time.sleep(2)
if __name__ == '__main__':
    with open("log.out", 'w') as out, open("log.err", 'w') as err:
        sys.stdout = out
        sys.stderr = err
        main()
I want the print statements to be written toe the stdout file after every line. I tried unsuccesfully:
python -u thisfile.py
In the bashrc (zshrc)
PYTHONUNBUFFERED = 1 
open("log.out", 'wb', buffering = 0) 
Changing the main function is no an option for the real case. A solution where I run the python file with bash and redirect errors and output is ok tough.
I know theres lots of questions like this, but none seem to work for me.
The solution should work ideally for python 3.8 and anything newer.
 
     
     
    