【问题标题】:My code doesn't continue until the program open by subprocess is closed在子进程打开的程序关闭之前,我的代码不会继续
【发布时间】:2021-11-25 20:37:50
【问题描述】:

我正在尝试使用 Subprocess 模块打开记事本,然后打印程序已打开。 我的代码:

import os
import subprocess
if os.path.exists(r'C:\Windows\System32\notepad.exe'):
    subprocess.run(r'C:\Windows\System32\notepad.exe')
    print('Program executed')

记事本打开,但问题是在我关闭记事本之前打印命令不起作用。有没有办法克服这个问题?

【问题讨论】:

  • subprocess.run() 阻塞,直到命令完成。使用线程、多处理或任何异步选项(或分离您正在运行的命令)

标签: python subprocess


【解决方案1】:

使用 Popen 代替 run 会有所帮助。然后脚本不等待程序关闭:

subprocess.Popen(r'C:\Windows\System32\notepad.exe')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    相关资源
    最近更新 更多