0

I have a program which I want to execute in for loops with changing input.

import subprocess
for num in range(StartingPoint, EndingPoint):
 p = subprocess.Popen("C:\\Programming\\simple\\Simple_C\\bin\\Simple_C.exe",
                   shell=True,
                   stdin=subprocess.PIPE,
                   stdout=subprocess.PIPE)
 p.communicate(input='%d\n' % num)
output = p.communicate()[0]
print (output)

But I get this error:

TypeError: 'str' does not support the buffer interface

The program asks for a number and Python should give it "num", is there a better solution to this? I am using Python version 3.3.2.

2 Answers 2

1

Use bytes instead

import subprocess
for num in range(StartingPoint, EndingPoint):
    p = subprocess.Popen("C:\\Programming\\simple\\Simple_C\\bin\\Simple_C.exe",
                shell=True,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE)
    p.communicate(input=('%d\n'%num).encode())
    output = p.communicate()[0]
    print (output)
Sign up to request clarification or add additional context in comments.

Comments

0

According the python documentation you should pass bytes instead of string:

The type of input must be bytes or, if universal_newlines was True, a string.

Warning: If you encode non-asccii text, ensure you are using the correct encoding when converting to bytes.

Comments

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.