0

I have a simple python file on a Linux VM

import time
while True:
    print("test")
    time.sleep(10)

which I start with

python test.py  > test.log

but even after a longer time that file is empty

-rwxrwx--- 1 root vboxsf      0 Sep 29 07:57 test.log

Is the output cached or something? How can I make the output to go into the file right away?

I am pretty sure I forgot something very easy...

1
  • 1
    This is a LInux thing. When stdout is redirected to a file, by default it is buffered. There are several ways around it. You can add flush=True to the print call, or you can disable buffering on sys.stdout by using the -u flag when you run Python. Commented Sep 29, 2023 at 6:04

1 Answer 1

1

change print like below

print("test", flush=True)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.