从 subprocess.Popen 获取整个输出

发布于 2024-09-28 02:01:35 字数 1207 浏览 1 评论 0原文

我通过调用 subprocess.Popen 得到了一个有点奇怪的结果,我怀疑这与我对 Python 的陌生有很大关系。

args = [ 'cscript', '%USERPROFILE%\\tools\\jslint.js','%USERPROFILE%\\tools\\jslint.js' ]
p = Popen(args, stdout=PIPE, shell=True).communicate()[0]

输出如下所示(尾随双 \r\n 是为了以防万一它很重要)

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.\r\n\r\n

如果我从交互式 Python shell 运行该命令,它看起来像这样

>>> args = ['cscript', '%USERPROFILE%\\tools\\jslint.js', '%USERPROFILE%\\tools\jslint.js']
>>> p = subprocess.Popen(args, stdout=subprocess.PIPE, shell=True).communicate()[0]
Lint at line 5631 character 17: Unexpected /*member 'OpenTextFile'.
f = fso.OpenTextFile(WScript.Arguments(0), 1),

...

Lint at line 5649 character 17: Unexpected /*member 'Quit'.
WScript.Quit(1);

所以这就是我真正关心的所有输出,但是如果我转储我刚刚设置的“p”变量的值...

>>> p
'Microsoft (R) Windows Script Host Version 5.8\r\nCopyright (C) Microsoft Corpor
ation. All rights reserved.\r\n\r\n'
>>>

我想要的所有数据最终去了哪里?它绝对不会以“p”结束。看起来它会输出到标准输出,但我没有明确告诉它不要这样做?

我在 Windows 7 x64 上使用 Python 2.6.6 运行它

I'm getting a slightly weird result from calling subprocess.Popen that I suspect has a lot to do with me being brand-new to Python.

args = [ 'cscript', '%USERPROFILE%\\tools\\jslint.js','%USERPROFILE%\\tools\\jslint.js' ]
p = Popen(args, stdout=PIPE, shell=True).communicate()[0]

Results in output like the following (the trailing double \r\n is there in case it's important)

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.\r\n\r\n

If I run that command from an interactive Python shell it looks like this

>>> args = ['cscript', '%USERPROFILE%\\tools\\jslint.js', '%USERPROFILE%\\tools\jslint.js']
>>> p = subprocess.Popen(args, stdout=subprocess.PIPE, shell=True).communicate()[0]
Lint at line 5631 character 17: Unexpected /*member 'OpenTextFile'.
f = fso.OpenTextFile(WScript.Arguments(0), 1),

...

Lint at line 5649 character 17: Unexpected /*member 'Quit'.
WScript.Quit(1);

So there's all the output I really care about, but if I dump the value of the "p" variable I just set up...

>>> p
'Microsoft (R) Windows Script Host Version 5.8\r\nCopyright (C) Microsoft Corpor
ation. All rights reserved.\r\n\r\n'
>>>

Where'd all that data I want end up going? It definitely didn't end up in "p". Looks like it's going to stdout, but I didn't I explictly tell it not to do that?

I'm running this on Windows 7 x64 with Python 2.6.6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月苍老的讽刺 2024-10-05 02:01:35

它会进入 stderr 吗?尝试重定向:

p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True).communicate()[0]

Is it going to stderr? Try redirecting:

p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True).communicate()[0]
少跟Wǒ拽 2024-10-05 02:01:35

正如 SimonJ 所建议的,它可能会被发送到 stderr

另外, docs 说不要使用 shell=True适用于您的情况的 Windows:

可执行参数指定
要执行的程序。这是非常罕见的
需要:通常,该程序
执行由args定义
争论。如果 shell=True,则
可执行参数指定哪个
外壳来使用。在 Unix 上,默认
外壳是/bin/sh。在 Windows 上,
默认 shell 由
COMSPEC 环境变量。唯一的
您需要指定的原因
shell=True 在 Windows 上是
您要执行的命令是
实际上内置于外壳中,例如
目录,复制。你不需要 shell=True
运行批处理文件,也不运行
基于控制台的可执行文件。


后来:哦等等。您是否使用 shell 来扩展这些环境变量?好吧,我收回刚才的话:你确实需要 shell=True

It's probably going to stderr, as SimonJ suggested.

Also, the docs say not to use shell=True in Windows for your case:

The executable argument specifies the
program to execute. It is very seldom
needed: Usually, the program to
execute is defined by the args
argument. If shell=True, the
executable argument specifies which
shell to use. On Unix, the default
shell is /bin/sh. On Windows, the
default shell is specified by the
COMSPEC environment variable. The only
reason you would need to specify
shell=True on Windows is where the
command you wish to execute is
actually built in to the shell, eg
dir, copy. You don’t need shell=True
to run a batch file, nor to run a
console-based executable.


Later: oh wait. Are you using the shell to get those environment variables expanded? Okay, I take it back: you do need the shell=True.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文