如何重定向子进程modu的stderr

2024-06-16 12:30:24 发布

您现在位置:Python中文网/ 问答频道 /正文

根据提供的答案here,我想将错误保存为字符串:

p = subprocess.Popen(['ding', 'dong'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
output, errors = p.communicate()

但是,重定向stderr似乎不起作用:

>>> p = subprocess.Popen(['ding', 'dong'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib64/python2.4/subprocess.py", line 550, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.4/subprocess.py", line 993, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

将错误捕获为字符串的正确方法是什么?你知道吗


Tags: 字符串inpyusr错误stderrstdoutline
1条回答
网友
1楼 · 发布于 2024-06-16 12:30:24

问题中的错误消息不是来自子进程。它是在执行子流程之前生成的。不能使用stderr选项捕获该错误。你知道吗

确保路径中有ding程序。你知道吗

相关问题 更多 >