【问题标题】:using python subprocess.popen..can't prevent exe stopped working prompt [duplicate]使用python subprocess.popen ..无法阻止exe停止工作提示[重复]
【发布时间】:2014-06-09 23:28:48
【问题描述】:

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

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

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

【问题讨论】:

    标签: python windows subprocess


    【解决方案1】:

    This site 似乎有解决方案。基本上,它说在脚本的开头包含这个:

    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
    

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

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

    MSDN 站点有一个page,它定义了许多类型的创建标志。希望这会有所帮助。

    【讨论】:

    • 在我的脚本中包含此内容时,我收到一条错误消息:`subprocess_flags = CREATE_NO_WINDOW NameError: name 'CREATE_NO_WINDOW' is not defined`
    • 我添加了定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多