I am pretty new to python and I am stuck with something now. I am using python 2.7. I am trying to automate a command in shell for which I wrote a python script. I have to input an integer value to the shell for which I am using Popen.communicate(input='2'). Though input is passed, it is passed as a string and the subprocess needs it as a numeric value. When I try to pass it as a numberic value like Popen.communicate(input=2) it throws the following error :
TypeError: 'int' object has no attribute '__getitem__'
So is there any way to send the input as a numeric value ??
Here is the code I use :
import sys
from subprocess import Popen, PIPE, STDOUT
cmd = ["sudo", "./sbt", "project java-examples", "run"]
proc = Popen(cmd, bufsize=4096, shell=False, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
string = proc.communicate(input='2')
print string[0]
proc.stdin.close()
Edit : Sorry that I didn't mentioned this at the first place. My java application has multiple main classes hence the compiler ask which class to be executed, and I need to enter the same number every time,which is why I am trying to automate it. When I am passing a string value it throws numberformatexception.
Finally I found the solution for this. Well the solution to this was pretty simple
when I changed
string = proc.communicate(input='2')
to
string = proc.communicate(input='2\n')
it worked fine.