当我使用os.system()或subprocess.call()时,如何隐藏控制台? [英] How do I hide the console when I use os.system() or subprocess.call()?
问题描述
我写了如下的一些语句:
os.system(cmd)#do something
subprocess.call ('taskkill / F / IM exename.exe')
p>
如何阻止它弹出控制台?
STARTUPINFO
可以隐藏控制台窗口:
si = subprocess.STARTUPINFO()
si.dwFlags | = subprocess.STARTF_USESHOWWINDOW
#si.wShowWindow = subprocess.SW_HIDE#default
subprocess.call('taskkill / F / IM exename .exe',startupinfo = si)
或设置创建标志禁用创建窗口: P>
CREATE_NO_WINDOW = 0x08000000
subprocess.call('taskkill / F / IM exename.exe',creationflags = CREATE_NO_WINDOW)$ b上面仍然是一个控制台进程,有控制台I / O的有效句柄(通过调用<$ c $来验证)$ b
< c> GetFileType 在 GetStdHandle
返回的句柄上)。它只有没有窗口,并不继承父级的控制台(如果有)。
您可以通过强制孩子完全没有控制台来进一步:
DETACHED_PROCESS = 0x00000008
subprocess.call('taskkill / F / IM exename.exe',creationflags = DETACHED_PROCESS)
在这种情况下,子进程的标准句柄(即 GetStdHandle
)为0,但可以将其设置为打开的磁盘文件或管道例如 subprocess.DEVNULL
(3.3)或 subprocess.PIPE
。
I wrote some statements like below:
os.system(cmd) #do something
subprocess.call('taskkill /F /IM exename.exe')
both will pop up a console.
How can I stop it from popping up the console?
The process STARTUPINFO
can hide the console window:
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#si.wShowWindow = subprocess.SW_HIDE # default
subprocess.call('taskkill /F /IM exename.exe', startupinfo=si)
Or set the creation flags to disable creating the window:
CREATE_NO_WINDOW = 0x08000000
subprocess.call('taskkill /F /IM exename.exe', creationflags=CREATE_NO_WINDOW)
The above is still a console process with valid handles for console I/O (verified by calling GetFileType
on the handles returned by GetStdHandle
). It just has no window and doesn't inherit the parent's console, if any.
You can go a step farther by forcing the child to have no console at all:
DETACHED_PROCESS = 0x00000008
subprocess.call('taskkill /F /IM exename.exe', creationflags=DETACHED_PROCESS)
In this case the child's standard handles (i.e. GetStdHandle
) are 0, but you can set them to an open disk file or pipe such as subprocess.DEVNULL
(3.3) or subprocess.PIPE
.
这篇关于当我使用os.system()或subprocess.call()时,如何隐藏控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!