Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t让python子进程stdin在linux上工作_Python_Subprocess_Stdin_Popen - Fatal编程技术网

Can';t让python子进程stdin在linux上工作

Can';t让python子进程stdin在linux上工作,python,subprocess,stdin,popen,Python,Subprocess,Stdin,Popen,假设我在当前位置有一个目录调用h33/,我想删除它。在shell中,我输入rm-rih33,它就会消失。我用python写了: import subprocess proc = subprocess.Popen(['rm','-ri','h33'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) proc.communicate('yes') 如果该目录中没有任何文件,那么这将非常有效!所以,如果我运行相同的linux命令,我必须回答“是

假设我在当前位置有一个目录调用h33/,我想删除它。在shell中,我输入
rm-rih33
,它就会消失。我用python写了:

import subprocess
proc = subprocess.Popen(['rm','-ri','h33'],
    stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc.communicate('yes')
如果该目录中没有任何文件,那么这将非常有效!所以,如果我运行相同的linux命令,我必须回答“是”以进入文件夹,回答“是”以删除其中的单个文件,然后回答“是”以删除目录。于是我写道:

import subprocess
proc = subprocess.Popen(['rm','-ri','h33'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
for i in range(3):
    proc.communicate('yes')
。。。它不起作用!不知道为什么

rm: descend into directory ‘hr33’? rm: remove regular empty file ‘hr33/blank’? rm: remove directory ‘hr33’? Traceback (most recent call last):
  File "multiDel.py", line 6, in <module>
    proc.communicate("yes")
  File "/usr/lib/python2.7/subprocess.py", line 806, in communicate
    return self._communicate(input)
  File "/usr/lib/python2.7/subprocess.py", line 1377, in _communicate
    self.stdin.flush()
ValueError: I/O operation on closed file
rm:下降到目录“hr33”中?rm:是否删除常规空文件“hr33/blank”?rm:是否删除目录“hr33”?回溯(最近一次呼叫最后一次):
文件“multiDel.py”,第6行,在
过程通信(“是”)
文件“/usr/lib/python2.7/subprocess.py”,第806行
返回自我。\u通信(输入)
文件“/usr/lib/python2.7/subprocess.py”,第1377行,在
self.stdin.flush()
ValueError:对关闭的文件执行I/O操作

我想做的主要事情是能够使用子流程容纳多个输入(我希望这是有意义的)。请帮助我

为什么不在不回答任何问题的情况下强制删除目录:

import subprocess
proc = subprocess.Popen(['rm','-rf','h33'],
    stdin=subprocess.PIPE, stdout=subprocess.PIPE)
.communicate()
关闭管道并等待子流程退出,以便您最多可以调用一次。假设
rm
只是一些接受输入的交互式程序的一个示例(否则,按照注释中的建议使用
shutil.rmtree()
rm-rf
):

或者您可以直接写入
proc.stdin

from subprocess import Popen, PIPE

proc = Popen(['rm','-ri','h33'], stdin=PIPE)
for i in range(3):
    proc.stdin.write(b'yes\n')
    proc.stdin.flush()
proc.communicate() # close pipes, wait for exit
通常,您可能需要与子流程交互:

import pexpect # $ pip install pexpect

pexpect.run("rm -ri h33", events={r"'\?": "yes\n"}, timeout=30)

它假设每个需要回答
yes
的提示都以
'结尾?

shutil.rmtree()有什么问题?那么为什么要使用
-i
开关呢?只需删除该选项,如果有必要,添加
-f
,系统不会询问您。但最重要的是,为什么不使用
shutil.rmtree()
?Python已经有了您需要的工具!最后但并非最不重要的一点是,您可能正在寻找
pexpect
来处理与子流程的“交互式”通信。我认为这是一个误解。rm命令只是一个例子。我真正想做的是用python处理交互式通信。我明天会调查一下
import pexpect # $ pip install pexpect

pexpect.run("rm -ri h33", events={r"'\?": "yes\n"}, timeout=30)