i pretty new python , stuck now. using python 2.7. trying automate command in shell wrote python script. have input integer value shell using popen.communicate(input='2'). though input passed, passed string , subprocess needs numeric value. when try pass numberic value popen.communicate(input=2) throws following error :
typeerror: 'int' object has no attribute '__getitem__'
so there way send input numeric value ??
here code use :
import sys 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 didn't mentioned @ first place. java application has multiple main classes hence compiler ask class executed, , need enter same number every time,which why trying automate it. when passing string value throws numberformatexception.
finally found solution this. solution pretty simple
when changed
string = proc.communicate(input='2')
to
string = proc.communicate(input='2\n')
it worked fine.
Comments
Post a Comment