1

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.

Doris
  • 63
  • 4

1 Answers1

1

its generally not recommended to use os.system and instead they encourage you to use subprocess.Popen or other methods ...

def exe_run():
    return subprocess.check_output(['./test.exe'])

now anything that is written to sys.stdout (ie print("asd")) will be returned to the caller

exe_output = exe_run() 
# now do whatever you want with it...
print(f"------EXE OUTPUT-----\n{exe_output}\n-------END OUTPUT ----")

if you want to just write to a file and make sure it is available you can use flush

with open("log.txt","wb") as fh:
    for i in range(2000):
       fh.write(f"{i},")
       fh.flush() # flush the output
       time.sleep(0.001)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Hi Sir, thanks a lot for answering it. Can I ask how can I save the output to file in real time? Cuz based on my understanding, if I run exe_run() first, followed by f.write(), it's like printing out data in terminal first, once it's finished it's then saved to file? – Doris Feb 07 '22 at 08:34