是否将popen子进程上的stdin重定向到原始输入?对于python 2.7
如何将是否将popen子进程上的stdin重定向到原始输入?对于python 2.7,python,python-2.7,subprocess,Python,Python 2.7,Subprocess,如何将subprocess.Popen中的stdin重定向到原始输入?我试图创建一个继承文件类的类,但它说:读取属性是“只读的” 我的代码: def read4(self,x): x=raw_input('=>') return x def process(cmd): cmd=cmd.strip().split(' ') px.os.environ["PYTHONUNBUFFERED"] = "1" process = subprocess.Po
subprocess.Popen
中的stdin重定向到原始输入?我试图创建一个继承文件类的类,但它说:读取属性是“只读的”
我的代码:
def read4(self,x):
x=raw_input('=>')
return x
def process(cmd):
cmd=cmd.strip().split(' ')
px.os.environ["PYTHONUNBUFFERED"] = "1"
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,stdin=subprocess.PIPE
)
process.stdin.read=read4
return process
def output(process):
while True:
if stop:
subprocess.Popen("TASKKILL /F /PID {pid} /T".format(pid=process.pid))
break
out = process.stdout.read(1)
if out == '' and process.poll() != None:
break
if out != '':
yield out
#sys.stdout.write(out)
#sys.stdout.flush()
p=process('python hello.py')
for z in output(p):
print z
hello.py
print 'hello'
x=raw_input('enter: ')
print x
你不能。引述自:
stdin
、stdout
和stderr
指定执行程序的标准
输入、标准输出和标准错误文件句柄分别为。
有效值为管道
,一个现有的文件描述符(正
整数),现有文件对象,以及None
因此,您不能将函数或随机对象用于stdin
。您可以简单地让子流程使用您当前的流程stdin,但没有内置的方式在每行输入之前自动显示提示
但是,您可以使用管道
手动执行此操作。从父进程stdin读取一行输入,然后将其写入子进程stdin并重复。我看到您在read4
中有self
,但是您创建的类在哪里?请检查问题的格式。此外,确保你的问题(摘录a)中没有不需要证明问题的无关内容。如果你还没有读过,就拿着书读一读。