【问题标题】:Python's subprocess: How do I hide the terminal window and still capture the output?Python 子进程:如何隐藏终端窗口并仍然捕获输出?
【发布时间】:2014-04-23 06:21:30
【问题描述】:

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

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。

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

谢谢, 尼克。

【问题讨论】:

  • 您是否尝试将父脚本另存为.pyw

标签: python-2.7 subprocess


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-04
    • 2017-09-17
    • 2019-04-10
    • 2010-10-21
    • 2021-01-26
    • 2016-07-21
    • 2012-07-01
    相关资源
    最近更新 更多