如何在 Python 中隐藏子进程的输出而不阻止其他子进程 运行
How to hide output of subprocess in Python without preventing other subprocesses from running
我在 运行 Python 3.3.5 Windows 上,我正在做的事情的一个非常基本的版本如下所示:
import os
import subprocess
import time
path = r"C:\Users\Luke\Desktop\Tor_Browser\Browser\firefox.exe {0}"
url = "http://google.com"
views = 5
opened = 0
for i in range(views):
subprocess.Popen(path.format(url))
time.sleep(15)
opened = opened + 1
print ("Times opened:", opened)
os.system("taskkill /f /im firefox.exe")
这段代码应该做的是 运行 我的 firefox/tor 浏览器 google url 5 次,每次 15 秒,然后关闭它。
它就是这样做的,但是,我似乎无法阻止终端显示文本:"SUCCESS: The process "firefox.exe" with PID xxxx has been terminated."
我试过换行
os.system("taskkill /f /im firefox.exe")
至
FNULL = open(os.devnull, 'w')
subprocess.Popen("taskkill /f /im firefox.exe", stdout=FNULL)
但是当我这样做时程序只打开浏览器一次,关闭它然后不再打开它但仍然显示 "Times opened" 文本。
有人知道如何阻止终端显示此文本吗?
谢谢
-卢克
试试这个:
import os
import subprocess
import time
path = r"C:\Users\Luke\Desktop\Tor_Browser\Browser\firefox.exe {0}"
url = "http://google.com"
views = 5
opened = 0
for i in range(views):
proc = subprocess.Popen(path.format(url)) # Change here to bind pipe to a name
time.sleep(15)
opened = opened + 1
print ("Times opened:", opened)
proc.terminate() # send process term signal
我在 运行 Python 3.3.5 Windows 上,我正在做的事情的一个非常基本的版本如下所示:
import os
import subprocess
import time
path = r"C:\Users\Luke\Desktop\Tor_Browser\Browser\firefox.exe {0}"
url = "http://google.com"
views = 5
opened = 0
for i in range(views):
subprocess.Popen(path.format(url))
time.sleep(15)
opened = opened + 1
print ("Times opened:", opened)
os.system("taskkill /f /im firefox.exe")
这段代码应该做的是 运行 我的 firefox/tor 浏览器 google url 5 次,每次 15 秒,然后关闭它。 它就是这样做的,但是,我似乎无法阻止终端显示文本:"SUCCESS: The process "firefox.exe" with PID xxxx has been terminated."
我试过换行
os.system("taskkill /f /im firefox.exe")
至
FNULL = open(os.devnull, 'w')
subprocess.Popen("taskkill /f /im firefox.exe", stdout=FNULL)
但是当我这样做时程序只打开浏览器一次,关闭它然后不再打开它但仍然显示 "Times opened" 文本。
有人知道如何阻止终端显示此文本吗?
谢谢
-卢克
试试这个:
import os
import subprocess
import time
path = r"C:\Users\Luke\Desktop\Tor_Browser\Browser\firefox.exe {0}"
url = "http://google.com"
views = 5
opened = 0
for i in range(views):
proc = subprocess.Popen(path.format(url)) # Change here to bind pipe to a name
time.sleep(15)
opened = opened + 1
print ("Times opened:", opened)
proc.terminate() # send process term signal