サブプロセスを使用してスクリプトを実行し、パイプでスクリプトの出力を取得し、出力で処理します。
スクリプトの最後まで読み取られる場合と、最後まで読み取れない場合があるという奇妙な問題が発生します。
これはバッファサイズの問題である可能性があると思います..いくつかの代替案を試しましたが、まだ成功していません..
def main():
x = subprocess.Popen('./autotest', bufsize = 1, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, cwd = '/home/vijay/run/bin', shell = True)
with open("out.txt",'wb') as f:
for line in x.stdout:
if 'Press \'q\' to quit scheduler' in line:
print line.strip()
f.write(line.strip())
x.stdin.write('q')
f.write('\n')
x.stdin.close()
x.stdout.flush()
try:
x.stdout.read()
except:
print 'Exception Occured !!!'
os._exit(1)
else:
print line.strip()
f.write(line.strip())
f.write('\n')
x.stdout.flush()
if __name__ == '__main__':
main()