2
私はPythonでbashセッションを開き、それが端末であるかのように対話し続けたいと思います。私はこの出力を取得するgitのクローン中に、Pythonでbashセッションを開いて通信する方法は?
import subprocess as s
class Session:
proc = None
def execute(self, cmd):
if self.proc is None:
self.proc = s.Popen("/bin/bash", shell=True, stdin=s.PIPE, stdout=s.PIPE, stderr=s.STDOUT)
cmd = cmd + ' ; echo EOF\n'
self.proc.stdin.write(cmd.encode())
output = ''
while not output.endswith("EOF"):
output += self.proc.stdout.read(1).decode()
return output
sess = Session()
sess.execute("export MY_VAR=HELLO")
sess.execute("git clone https://github.com/internetsadboy/crapi.git")
しかし、それは
Cloning into 'crapi'...
EOF
は、あなたが/ binに/ bashのを実行しようとしたことがありますか?それ以降、コマンドをパイプすることができます – dGRAMOP
可能な重複:[Pythonを使った対話型入出力](https://stackoverflow.com/questions/19880190/interactive-input-output-using-python) – John1024
'export MY_VAR = HELLO'はサブプロセスで動作しないと思います –