Temmis
Temmis

Reputation: 1

Excecute program with input

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.

Upvotes: 0

Views: 57

Answers (2)

gecco
gecco

Reputation: 18850

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.

Upvotes: 0

simonzack
simonzack

Reputation: 20928

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)

Upvotes: 1

Related Questions