- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
[已编辑]
用于处理 subprocess.Popen
标准输出的 2 个选项是 stdout="a_file_name"
和 stdout=subprocess.PIPE
。stderr
可以通过 stderr=subprocess.STDOUT
与其中任何一个组合。
对于我目前正在做的事情(模糊测试),我生成的 stdout="a_file_name"
代码稍微更短、更清晰。
但是,据我所知,stdout=PIPE
似乎经常受到其他人的青睐,但我不确定其中的所有原因。
如果 Popen([cmd, arg], ...)
中使用的 cmd
是外部可执行文件,它将错误输出写入 stderr
,stdout=PIPE
比 stdout="a_file_name"
更好吗?
各自的优点和缺点是什么?
stdout=PIPE
而不是 stdout="a_file_name"
的 1 个优点是前者会让我轻松地跳过编写空文件。cmd
崩溃,这 2 个中的 1 个是否更有可能以某种方式获得所有错误输出?尽管我考虑到了特定的背景,但我也有兴趣了解更一般情况的答案。
为了更好地解释我的上下文,这里是我的 2 个替代代码段:
import subprocess
import sys
assert sys.version_info >= (3, 3)
# timeout added for subprocess's wait() and communicate() in Python 3.3.
with open('sub_proc1.outerr', 'w') as f_outerr1:
sub_proc1 = subprocess.Popen([cmd, args], stdout=f_outerr1,
stderr=subprocess.STDOUT,
universal_newlines=True)
try:
return_code = sub_proc1.wait(timeout=10)
print('*** %s CRASHED with return code: %d.' % (cmd, return_code))
except subprocess.TimeoutExpired:
print('*** %s succeeded.' % cmd)
sub_proc1.kill()
对比:
...
with open('sub_proc2.outerr', 'w') as f_outerr2:
sub_proc2 = subprocess.Popen([cmd, args], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
try:
(sub_proc2_out, sub_proc2_err) = sub_proc2.communicate(timeout=10)
print('*** %s CRASHED with return code: %d.' %
(cmd, sub_proc2.poll()))
assert sub_proc2_err is None
# Since stderr was redirected to STDOUT, this should be None.
f_outerr2.write(str(sub_proc2_out or ""))
# Treat 'None' as an empty string).
except subprocess.TimeoutExpired:
print('*** %s succeeded.' % cmd)
sub_proc2.kill()
原帖:
TITLE: subprocess: Pros and cons of stderr=STDOUT vs. stderr=PIPE?
The 2 principal alternatives for handling error output from 'subprocess.Popen' seem to be 'stderr=STDOUT' (with 'stdout="some_file"') and 'stderr=PIPE'.
For what I want to do (fuzz-testing), my resulting 'stderr=STDOUT' code is a little shorter and cleaner.
However, from what I've read, it seems that 'stderr=PIPE' is preferred, but I'm not sure of all of the reasons why.
If the 'cmd' used is an external executable which writes error output to 'stderr', what are the pros and cons of using 'stderr=STDOUT' versus 'stderr=PIPE'?
...
最佳答案
写入特定文件意味着如果一次运行多次,您的程序将会发生冲突,因为两个进程都想要写入同一个文件。 (搜索临时文件创建问题和安全漏洞)
使用管道意味着不存在文件名唯一性问题。
你关心输出吗?如果没有,则使用 subprocess.DEVNULL ,它会为您丢弃输出。
关于python - 对于 subprocess.stderr=STDOUT - stdout=PIPE 比 stdout ="a_file_name"更好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35710305/
我有以下代码来捕获和处理运行命令输出。 如何修改它以便运行命令窗口显示输出并同时记录输出?更换 @SW_HIDE与 @SW_SHOW (或等效的)只显示一个空白的命令窗口。 类似于linux的东西te
[已编辑] 用于处理 subprocess.Popen 标准输出的 2 个选项是 stdout="a_file_name" 和 stdout=subprocess.PIPE。 stderr 可以通过
c99 的 7.19.3/7 节指出: At program start-up, three text streams are predefined and need not be opened ex
我正在运行以下 proc_open 函数。加载页面时,出现错误: Use of undefined constant STDOUT - assumed 'STDOUT'` 如何正确设置STDOUT和S
我有一个运行多个进程的开发堆栈:网络服务器、自动测试、后台编译等。所有这些都是基本的命令行命令,例如 node app.js 或 lein midje :autotest. 是否可以使用一个脚本在“后
我正在使用 SLURM 在 super 计算机上调度作业。我已设置 --output=log.out 选项,将作业标准输出中的内容放入文件 (log.out)。我发现该文件每 30-60 分钟更新一次
ansible/ansible-playbook 版本:2.1.2.0/2.2.0.0 我正在尝试使用 yum/apt 安装软件包,但由于安装软件包的存储库位于 packagecloud.io 中,有
bala@hp:~$ echo "Hello World" > stdout bala@hp:~$ cat stdout Hello World bala@hp:~$ echo "Hello Worl
在从 Fortran 编写的外部库中调用嘈杂的函数之前,我正在使用如下代码重定向标准输出: // copy standard output out = dup(STDOUT_FILENO); // c
这个问题在这里已经有了答案: How can I pipe stderr, and not stdout? (11 个答案) 关闭 6 年前。 我有一个程序,我想检查其 STDERR 输出并在其上运
我正在从 perl 运行一个 java 应用程序 这是脚本 #!/usr/bin/perl use strict; use warnings; $| = 1; my $args = join (" "
我正在尝试将 python 脚本的 STDOUT 重定向到一个文件。 如果 STDOUT 是从 sys 导入的,脚本的输出不会被重定向到一个文件: from sys import stdout std
我正在尝试使用 PHP 和 Apache(在 Docker 的前台运行)写入 stdout(或 stderr)。 这些作品: file_put_contents( "php://stderr","wo
我正在尝试重定向标准输出,以便 Windows 应用程序中的 printf 将转到我选择的文件。 我这样做: outFile = fopen("log.txt", "w"); *stdout = *o
在 Ruby 中,$stdout(前面有一个美元符号)和 STDOUT(全部大写)有什么区别?在进行输出重定向时,应该使用哪个,为什么? $stderr 和 STDERR 也是如此。 编辑: 刚找到一
让我们看看这个Hello World程序 #include int main(int argc, char ** argv) { printf("Hello, World!"); c
我在 64 位机器上运行 Ubuntu 20.04。 我想将 stdout 重定向到从 memfd_create 获得的描述符。似乎使用 memfd_create 创建的匿名文件只有在 stdout
我想在 Python 脚本末尾捕获控制台输出。也就是说,我既想正常打印到控制台,又想在执行结束时将控制台输出保存到文件中。 我看过各种相关的 SO 问题 1 , 2 , 3尽管他们要么简单地重定向输出
我知道可以将两者都重定向到特定文件: ./command 1> out.log 2> err.log 或 ./command 1>test.log 2>&1 将两者写入文件。但是我不知道在只打印其中一
我知道可以将两者都重定向到特定文件: ./command 1> out.log 2> err.log 或 ./command 1>test.log 2>&1 将两者写入文件。但是我不知道在只打印其中一
我是一名优秀的程序员,十分优秀!