简体   繁体   English

发生异常时,Python Pandoc子进程不打印STDOUT

[英]Python Pandoc Subprocess Not Printing STDOUT When Exception Occurs

I'm trying to run a process with subprocess and print its entire output if and only if an exception occurs. 我正在尝试使用子进程运行一个进程,并且仅当发生异常时才打印其整个输出。

Where I was before: 我以前去过的地方:

try:
    proc = subprocess.run(
        command,
        capture_output=True,
        check=True,
        text=True,
    )
except subprocess.CalledProcessError as error:
    print(error.output)

This did not work. 这没有用。

Output when subprocess.CalledProcessError occurs: 发生subprocess.CalledProcessError时的输出:

b'' 

Replacing capture_output with stdout=subprocess.PIPE resulted in the output of everything regardless whether an exception occurred or not, error.output was still empty. 用stdout = subprocess.PIPE替换capture_output会导致一切输出,无论是否发生异常,error.output仍然为空。

So I experimented: 所以我做了实验:

This prints everything I would see if I executed the command in the command-line. 如果我在命令行中执行了命令,这将打印出我将看到的所有内容。

subprocess.run(
    command,
    stdout=subprocess.PIPE,
)

This prints out nothing. 这不会打印任何内容。

proc = subprocess.run(
    command,
    capture_output=True,
)
print(proc.stdout.decode())

I also tried subprocess.check_output() which to my information does the same as subprocess.run() with the flags I set in the first code snippet. 我还尝试了subprocess.check_output(),据我所知它与subprocess.run()相同,但带有在第一个代码段中设置的标志。

What am I missing here? 我在这里想念什么? Thanks. 谢谢。

Addendum 附录

import subprocess

command = ['pandoc', 'file']

try:
    proc = subprocess.run(
        command,
        capture_output=True,
        check=True,
    )
except subprocess.CalledProcessError as error:
    print('Exception:')
    print(error.output)

This is an MWE with the specific process I want to run ( pandoc ) 这是具有我要运行的特定流程的MWE( pandoc

Output 产量

$ pandoc file
pandoc: file: openBinaryFile: does not exist (No such file or directory)

$ ./samplecode.py
Exception:
b''

So the exception gets triggered, but the output object is empty. 因此异常被触发,但是输出对象为空。

It seems that the error message is present in error.stderr and not in error.output. 似乎错误消息存在于error.stderr中,而不存在于error.output中。 I tried your example (with a ls of non-existent file) : 我尝试了您的示例(带有ls不存在的文件):

import subprocess

command = ['ls', 'file']

try:
    proc = subprocess.run(
        command,
        check=True,
        capture_output=True,
        text=True
    )
except subprocess.CalledProcessError as error:
    print('Exception:')
    print('output : ' + error.output)
    print('stderr : ' + error.stderr)

The output is the following : 输出如下:

Exception:
output : 
stderr : ls: file: No such file or directory

Hope it helps. 希望能帮助到你。

I believe what you're meaning to run is stderr=subprocess.PIPE . 我相信您要运行的意思是stderr=subprocess.PIPE This should print the relevant error code to the standard console error output. 这应该将相关的错误代码打印到标准控制台错误输出中。

Example: 例:

process = subprocess.Popen(['ls', 'myfile.txt'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output,error) = process.communicate()
if error:
    print error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM