Python的子进程:如何隐藏终端窗口并仍然捕获输出?

时间:2014-04-22 16:59:21

标签: python-2.7 subprocess

此代码有效,但会弹出一个终端窗口(简要):

print 'Trying wireless'
while True:
    wlan = subprocess.Popen("netsh wlan connect name='BSD'", stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    out, error = wlan.communicate()
    if out.find('success') >=0: break
    print "Still trying wireless..."
    time.sleep(0.5)
print "Connected!"

这是Windows 7上的Python 2.7。

有什么办法可以停止弹出并继续抓取输出?

谢谢, 尼克。

1 个答案:

答案 0 :(得分:1)

我没有这方面的设置,但由于没有答案,我发现了一些模糊的承诺in the docs。你能试试这个:

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESHOWWINDOW  # tell windows to use wShowWindow options
si.wShowWindow = subprocess.SW_HIDE  # ShowWindow option - only one that sounded useful
wlan = subprocess.Popen(...., startupinfo=si)  # as before but add the startupinfo argument