I want to ask a question how to read all print out data in terminal in real time, which means that when the data is printed out in terminal, it's saved in text file synchronously. The python code is like:
def exe_run():
os.system(r'"test.exe"')
return
def main_run():
#some code read from arduino serial port
#...
while True:
data = ser.readline()
print(data)
count += 1
if count % 3 == 0:
exe_run() #run def exe_run every 3 times of arduino signal
continue
result = main_run()
In the exe file, it will gives a set of data when calling this file. So the result in terminal is something like:
arduino_siganl_1
arduino_siganl_2
arduino_siganl_3
exe_data_1
arduino_siganl_1
... (repeat)
Above is what I've got so far. And my question is I want to save all signals to text file in the same order like terminal. However, due to some data are come from exe file, I can't use f.write
function. I tried sys.stdout
, but I don't know how to make the 'saving to text' task become in real time. If I do things like:
sys.stdout = open('/the/path/to/your/file', 'w')
result = main_run()
sys.stdout.close()
Seems data are not saved immediately when they are printed out, and some output are saved, some are not. Anyone has ideas how to achieve that? Thank you in advance.