I have a Python script I want to get information from a CPP .exe I have.
The .exe prints a string to console using printf.
I want to be able to get this string into my Python code.
I want to be able, in my Python code, to do something like this :
gyro_data = GetFromCommandLine(cpp.exe.run())
Also - my cpp.exe has a loop that runs forever and keeps feeding console with strings. Can this be done? Or do I have to break my cpp exe to return something instead of printing to screen all the time ?
-
I will edit my questionOfri– Ofri2019年02月20日 15:41:49 +00:00Commented Feb 20, 2019 at 15:41
1 Answer 1
I would suggest using the subprocess module.
You will then be able to do something like (this code is from their documentation, which requires Python 3.7)
subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
which returns
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
For earlier versions, you would use subprocess.Popen like this
your_process = Popen(['ls', '-l', '/dev/null'], stdout=subprocess.PIPE)
your_process.wait()
your_process_output = your_process.stdout().decode() # Here you will have a string
This snippet could very easily be wrapped into a single function.