在 os.startfile() 中运行时隐藏控制台?
我有一个带有参数的 .bat
文件;我使用 os.startfile(test.bat)
运行它.有没有办法隐藏它的控制台?我尝试使用 subprocess
,效果很好,但是当我关闭父程序时,使用 py2exe
控制台模式编译的 subprocess
也会关闭.>
I have a .bat
file with parameters; I run it using os.startfile(test.bat)
. Is there a way to hide its console? I tried using subprocess
, it works well but when I close the parent program the subprocess
that was compiled using py2exe
console mode closes too.
info = subprocess.STARTUPINFO()
info.dwFlags=1
info.wShowWindow=0
subprocess.Popen(test.bat,startupinfo=info)
谢谢
Use shell=True
and creationflags=subprocess.SW_HIDE
with subprocess.Popen
代码>.这对我有用
Use shell=True
and creationflags=subprocess.SW_HIDE
with subprocess.Popen
. This worked for me
subprocess.Popen(['test.bat'], shell=True, creationflags=subprocess.SW_HIDE)
在一些 Python 版本中,SW_HIDE
在 subprocess
模块中不可用.在这种情况下,您可能必须使用 _subprocess.SW_HIDE
In some releases of Python, SW_HIDE
is not available in subprocess
module. In that case, you may have to use _subprocess.SW_HIDE