2012-12-27 59 views
0

我正在Python中製作GUI應用程序,需要運行cmd提示命令。Python子流程模塊

e。克:

import subprocess 
subprocess.Popen("arp -a > arptable.txt",stdin=PIPE, stdout=PIPE,shell=Ture) 

現在,運行該命令後,我看到的cmd screen小飛濺,並生成包含arp表的文本文件。在我的應用程序中,我不希望這個黑屏出現,所以用戶不知道應用程序中涉及到cmd prompt

我該怎麼做?

回答

0

你不需要shell=True

import os 
from subprocess import PIPE, STDOUT, check_call 

with open("arptable.txt", "rb") as file, open(os.devnull, "r+b") as devnull: 
    check_call(["arp", "-a"], stdin=devnull, stdout=file, stderr=STDOUT) 
0

在Windows,你需要一個STARTUPINFO類傳遞到的subprocess.Popenstartupinfo參數來實現這一目標。爲了便攜:

import subprocess 
import os 

si = None 
if os.name == 'nt': 
    si = subprocess.STARTUPINFO() 
    si.dwFlags |= subprocess.STARTF_USESHOWWINDOW 
proc = subprocess.Popen("arp -a > arptable.txt",stdin=PIPE, stdout=PIPE, startupinfo=si)