python - 引发异常时,如何停止CMD(来自subprocess.popen)关闭?

标签 python windows error-handling subprocess popen

我正在使用subprocess.popen使用给定的python文件生成多个CMD。最后都带有input()。问题是,如果代码中有任何引发的异常,则窗口只是关闭而我看不到发生了什么。

无论错误如何,我都希望它保持打开状态。这样我就可以看到或因为此脚本无法运行而使错误返回主窗口。

我在Windows上运行它:

import sys
import platform
from subprocess import Popen,PIPE

pipelines = [("Name1","path1"),
         ("Name2","path2")]

# define a command that starts new terminal
if platform.system() == "Windows":
  new_window_command = "cmd.exe /c start".split()
else:  #XXX this can be made more portable
  new_window_command = "x-terminal-emulator -e".split()
processes = []
for i in range(len(pipelines)):
  # open new consoles, display messages
  echo = [sys.executable, "-c",
        "import sys; print(sys.argv[1]);  from {} import {}; obj = {}(); obj.run();  input('Press Enter..')".format(pipelines[i][1],pipelines[i][0],pipelines[i][0])]
  processes.append(Popen(new_window_command + echo + [pipelines[i][0]])) 

for proc in processes:    
  proc.wait()

最佳答案

要查看错误,请尝试将所需的代码片段包装在try/except

try:
    ...
except Exception as e:
    print(e)

关于python - 引发异常时,如何停止CMD(来自subprocess.popen)关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59983148/

相关文章:

r - 我如何在R中打印实际的错误消息

angular - 如何使控制台中显示的错误引用有 Angular cli项目中的 typescript 源映射?

python - 每个重试周期增加 celery 重试时间

Python:突破 SteamCMD 提示符

c# - TypeLoadException 隐藏内部异常

java - 如何用 Java 编写程序来编译 .asm(汇编语言)文件?

基于体系结构的 C# 结构行为

python - 在 Google App Engine 中使用 @ndb.tasklet 或 @ndb.synctasklet

python - 在 bash 的 for 循环中读取字符串数组

python - 将儒略日期转换为数据框中的正常日期?