我想subprocess.check_output()用ps -A | grep 'process_name'.我尝试了各种解决方案,但到目前为止没有任 有人可以指导我怎么做吗?
我需要做这样的帖子,但是我需要创建一个可以给出输入并多次输出的子进程.该帖子的接受答案有良好的代码......
from subprocess import Popen, PIPE, STDOUT
p = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
grep_stdout = p.communicate(input=b'one\ntwo\nthree\nfour\nfive\nsix\n')[0]
print(grep_stdout.decode())
# four
# five
Run Code Online (Sandbox Code Playgroud)
......我想继续这样:
grep_stdout2 = p.communicate(input=b'spam\neggs\nfrench fries\nbacon\nspam\nspam\n')[0]
print(grep_stdout2.decode())
# french fries
Run Code Online (Sandbox Code Playgroud)
但是,我得到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/subprocess.py", line 928, in communicate
raise ValueError("Cannot send input after starting communication")
ValueError: Cannot send input after starting communication
Run Code Online (Sandbox Code Playgroud)
如果我理解正确的话,proc.stdin.write()方法不能让你收集输出.什么是保持线路开放以进行持续输入/输出的最简单方法?
编辑:====================
它看起来像是pexpect一个有用的库,我正在尝试做什么,但我无法让它工作.这是对我实际任务的更完整的解释.我hfst用来获得个别(俄语)单词的语法分析.以下演示了它在bash shell中的行为:
$ hfst-lookup analyser-gt-desc.hfstol
> ?????
????? ?????+N+Neu+Inan+Sg+Acc …Run Code Online (Sandbox Code Playgroud) 我有一个很长的工作,运行几分钟,然后重新启动.该任务输出我捕获的各种信息:
output = subprocess.Popen(cmd,stdout=subprocess.PIPE).communicate()
Run Code Online (Sandbox Code Playgroud)
问题是,我一次只能得到整个输出.我想显示输出,因为程序将它发送到stdout,同时仍然将其推回缓冲区(我需要检查输出是否存在某些字符串).在Ruby中,我会这样做:
IO.popen(cmd) do |io|
io.each_line do |line|
puts line
buffer << line
end
end
Run Code Online (Sandbox Code Playgroud) 我想自动升级程序.
我用Python运行这段代码:
import subprocess
subprocess.call('./upgrade')
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我从shell获得升级程序成功启动的输出,然后我得到'按Enter继续'.我如何自动化这个过程,以便python脚本在发布时自动"按下"输入?我需要在手术过程中做两次.我需要在Linux上完成此操作,而不是Windows,因为它在这里被要求: 生成键盘事件 此外,这需要在Shell提示输入后专门完成.谢谢你的帮助.我在这里找不到解决方案: 按enter键作为命令输入
我有一个 python 脚本,需要在其中调用 shell 命令。shell 命令可以接受来自文件或标准输入的输入。
在我的脚本中,输入存储在变量中。调用命令的正确方法是什么?如果重要的话,shell 命令不会产生任何输出。
我知道我可以将变量的内容写入文件并使用该文件作为参数调用命令,但这似乎不优雅。当然,还有更好的方法。
有人能告诉我那是什么吗?谢谢。
假设我有一个简单的 python 脚本,它使用该模块执行 elixir/erlang 脚本subprocess。
假设 python 脚本的操作系统 PID 是P1,而运行的生成的 elixir/erlang 脚本的操作系统 PID 是P2。
我想知道 和 之间是否P1可以P2进行通信。更具体地说,向ofP1写入一些内容,然后从 of 读取接收到的输入,并将一些相应的输出写入其自己的输出,然后从of读取内容,然后再次向of写入一些内容,依此类推。stdinP2P2P1stdoutP1stdoutP2stdinP2
我知道另一种方式是可能的,即从 Elixir/erlang 内部生成外部进程,然后与该进程通信。任何帮助表示赞赏,谢谢。