Python uses string buffering when stdout is a tty device, hence the output matches the print order. If the python buffer is redirected, the output of the print statements is output. While C ++ executable output buffering is not handled by python in both cases. Therefore, when the output is redirected, print statements receive buffering and are not output to the file until the buffer is full or the program ends.
sys.stdout.flush() will clear the output of print statements as shown below. Please note that it must follow print statements.
#!/usr/local/bin/python import os import sys print("Hello1") print("Hello2") sys.stdout.flush() os.system("/python/cppprog.o") print("Bye1") print("Bye2")
Output:
]# python script.py > o.txt ]# cat o.txt Hello1 Hello2 Hello, world! Bye1 Bye2
user966588
source share