|在Subprocess.call中不起作用 [英] | Not Working In Subprocess.call

查看:81
本文介绍了|在Subprocess.call中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在带有"|"的子过程中使用命令时在它不工作,它的输出为命令"|"未知,请尝试在链接帮助中".或当我把它放进去时:

Whenever I use a command in a subprocess with "|" in it doesn't work it has an output of Command "|" is unknown, try "in link help". Or when I put this:

#!/usr/bin/python
from subprocess import call
from shlex import split

interface = call(split("ip -o link show | awk '{print $2}' | grep wl"))

它给出以下输出:

Error: either "dev" is duplicate, or "awk" is a garbage.

推荐答案

尽管我无法将两者链接在一起,但您可以使用 subprocess.check_output 方法和 Popen 类管道作业.部分解决方案:

You can use subprocess.check_output method and Popen class though I wasn't able to chain both pipe operations. Partial solution:

from subprocess import check_output, Popen, PIPE
from shlex import split

process = Popen(split('ip -o link show'), stdout=PIPE)
output = check_output(('awk', '{print $2}'), stdin=process.stdout)
return_code = process.wait()
print(output, return_code)

因此,基本上,awk将采用 process 标准输出,并将结果保存在 output 变量中.

So basically, awk is taking the process standard output, and result is saved in the output variable.

这篇关于|在Subprocess.call中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆