将os.system转换为subprocess.Popen

时间:2017-11-17 06:19:16

标签: python-2.7 cmd subprocess os.system

我正在编写一个脚本来打开一个新的命令提示符,并运行一个可执行文件。我使用os.system获得了一个有效的代码。你能帮我把它转换成subprocess.Popen吗?

hello_exe = r'c:\hello.exe'
os.system("start cmd /k {}".format(hello_exe))

我无法将hello_exe作为后台进程运行,因为我希望用户看到命令提示符并能够滚动日志。任何帮助表示赞赏。感谢。

我的环境:Windows8和python 2.7.12

2 个答案:

答案 0 :(得分:0)

转换为subprocess.Popen是这样的:

import subprocess

process = subprocess.Popen(r'c:\hello.exe', shell = True)
process.communicate()

答案 1 :(得分:0)

转换为Popen

from subprocess import Popen, PIPE

hello_exe = r'c:\hello.exe'

proc = Popen(hello_exe, stdout=PIPE, shell = True)
proc.communicate()

以下是一些您可以参考的链接,了解subprocess.Popen

Link to understand Popen

Another link to understand Popen

Link to 50+ examples of Popen

相关问题