0

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 ?

1
  • I will edit my question Commented Feb 20, 2019 at 15:41

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I will test it :)
So if my exe is constantly printing a string, example: quat 1.1 2.5 1.4 6.5 over and over again, the code you wrote will run for every entry ? or can I run it a single time for one entry ?
Also, I now get an exception "file object is not callable"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.