关于 Python 的 popen 无法获取 pipe 的输出,大家有什么头猪吗

2021-12-11 19:26:17 +08:00
 LeeReamond

使用 proc = popen("command")之后,使用 proc.stdout 获得标准输出流

但是经过测试发现如果 command 里面是比较简单的命令,比如 ping baidu.com 这种那程序就能正常运行,如果是包含有 A 程序 pipe 到 B 的命令,比如 ffmpeg pipe 到 x264.exe 输出就完全无法拿到。请问各位大佬这是咋回事

2814 次点击
所在节点    Python
26 条回复
Quarter
2021-12-11 19:41:21 +08:00
头猪,哈哈哈
Seahurt
2021-12-11 20:36:46 +08:00
官方文档写的挺清楚的了,推荐先看文档。无法从 proc.stdout 获取输出可能原因是命令的输出是写到了 stderr ,需要用 stderr=subprocess.STDOUT 来重定向。另外 A pipe 到 B 的命令 A 的输出肯定就没有了,只能拿到 B 命令的输出,除非用了 tee
chotow
2021-12-11 20:49:14 +08:00
难得见到一个不是拼音打字的朋友 🤣
LeeReamond
2021-12-11 20:50:42 +08:00
@Seahurt 想拿到 B 的输出,但是啥也没有啊
vanton
2021-12-11 21:08:13 +08:00
```python
import shlex, subprocess
import pprint

command_line = "ping baidu.com | grep 'TTL'"
args = shlex.split(command_line)

# proc = subprocess.Popen(["ping","baidu.com"])
proc = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE)

pprint.pp(args)

pprint.pp(proc.stdout.readlines())
```

你跑下看有没有东西
jaredyam
2021-12-11 21:11:08 +08:00
LeeReamond
2021-12-11 21:18:53 +08:00
@vanton ping 命令不能正常工作,提示

ping: usage error: Destination address required
[]
vanton
2021-12-11 21:40:41 +08:00
@LeeReamond #7

😱

这都跑不起来?

那你这环境有问题啊,你换台机子试下。
hsfzxjy
2021-12-11 22:30:06 +08:00
看到头猪在想楼主是不是也逛 A 岛 :doge
iluckypig
2021-12-12 00:16:07 +08:00
快过年了,杀猪吃肉!
LeeReamond
2021-12-12 01:14:33 +08:00
@vanton ssh 里的 ping 命令正常,我觉得是你代码传参有问题
Salicylicacid
2021-12-12 09:06:27 +08:00
续( xfnd )
Osk
2021-12-12 10:57:46 +08:00
@LeeReamond 上面 shell=True 的方式好像得注意 win/linux 平台的差异, 或者试试传递 args 为 string 试试.
unix 中 list+shell=True 时, args 的其余参数好像是传给 shell
Osk
2021-12-12 10:59:04 +08:00
On POSIX with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself.

On Windows with shell=True, the COMSPEC environment variable specifies the default shell. The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.
vanton
2021-12-12 11:50:10 +08:00
@LeeReamond #11
这个在 win11 powershell 下写的,mac 不是这么写的。
参数是 shlex 解析的,自动解析为当前操作系统可识别参数,所以参数没问题。

一般就是你的环境问题。
ClericPy
2021-12-12 12:39:01 +08:00
输出是 stderr 还是 stdout 上了? 反正这俩我都重定向到 PIPE 里然后迭代出来的
ncepuzs
2021-12-12 13:05:50 +08:00
@Salicylicacid 绪( xftj )
zmaplex
2021-12-12 14:17:46 +08:00
二进制文件填写完整路径。如 ping 127.0.0.1 写成 /usr/bin/ping 127.0.0.1
2i2Re2PLMaDnghL
2021-12-12 18:35:33 +08:00
@vanton
稍微 strace 了一下,你这段运行的是
["/bin/sh", "-c", "ping", "baidu.com", "|", "grep", "TTL"]
确实是参数有问题,解析完了再给 shell=True 你这不是逗呢
vanton
2021-12-12 23:44:12 +08:00
@2i2Re2PLMaDnghL #19

你看了问题没,他这是 Windows 。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/821556

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX