本文介绍了使用 python subprocess.popen..can't 阻止 exe 停止工作提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,需要一些帮助.我正在编写一个 python 脚本来调用一个应用程序 exe(比如 abc.exe).为此,我正在使用 subprocess.popen.例如:

I am a python newbie and need some help. I am writing a python script to call an application exe(say abc.exe). I am using subprocess.popen for this purpose. e.g. :

r_stdout = subprocess.Popen(CommandLine,
                                  stdout = subprocess.PIPE,
                                  stderr = subprocess.PIPE).communicate()[1]

CommandLine 是:abc.exe -options "".abc.exe 对我来说是一个黑盒子,它为我传递的某些选项生成错误提示.错误提示是标准的 Windows 提示,说 abc.exe 已停止工作,给了我 3 个选项来在线检查解决方案、关闭程序、调试程序.现在我的问题是:有没有办法避免这个命令提示符?即有没有办法让 python 脚本抑制这个提示?

the CommandLine here is : abc.exe -options "<optionstr>". abc.exe is a black box to me and it is producing an error prompt for some of the options I am passing. The error prompt is a standard windows' prompt saying abc.exe has stopped working, giving me 3 options to check online for solution, close program, debug program.Now my question is : Is there any way to avoid this command prompt ? i.e. is there a way for a python script to suppress this prompt ?

推荐答案

这个网站 似乎有解决方案.基本上,它说要在脚本的开头包含它:

This site seems to have the solution. Basically, it says to include this at the start of your script:

if sys.platform.startswith("win"):
    # Don't display the Windows GPF dialog if the invoked program dies.
    # See comp.os.ms-windows.programmer.win32
    #  How to suppress crash notification dialog?, Jan 14,2004 -
    #     Raymond Chen's response [1]

    import ctypes
    SEM_NOGPFAULTERRORBOX = 0x0002 # From MSDN
    ctypes.windll.kernel32.SetErrorMode(SEM_NOGPFAULTERRORBOX);
    CREATE_NO_WINDOW = 0x08000000    # From Windows API
    subprocess_flags = CREATE_NO_WINDOW
else:
    subprocess_flags = 0

在此之后,您将像这样执行您的子流程:

After this, you'd execute your subprocess like this:

r_stdout = subprocess.Popen(CommandLine,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            creationflags=subprocess_flags).communicate()[1]

MSDN 站点有一个页面,其中定义了许多创建标志的类型.希望这会有所帮助.

The MSDN site has a page which defines many types of creation flags. Hope this helps.

这篇关于使用 python subprocess.popen..can&amp;#39;t 阻止 exe 停止工作提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:45