找到你要的答案

Q:How to hide output of subprocess in Python without preventing other subprocesses from running

Q:如何隐藏进程Python输出无防止其他子过程的运行

I run Python 3.3.5 on Windows, and a very basic version of something I'm working on looks like this:

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")

What this code is supposed to do is run my firefox/tor browser with the google url 5 times for 15 seconds each and then close it. It does just this, however, I can't seem to stop the terminal from displaying the text: "SUCCESS: The process "firefox.exe" with PID xxxx has been terminated."

I've tried changing the line

os.system("taskkill /f /im firefox.exe")

to

FNULL = open(os.devnull, 'w')    
subprocess.Popen("taskkill /f /im firefox.exe", stdout=FNULL)

but when I do the program only opens the browser once, closes it and then ceases to open it again but still displays the "Times opened" text.

Anyone have any ideas as to how I can stop the terminal from displaying this text?

Thanks

-Luke

我运行Python 3.3.5在Windows,和我正在做的工作看起来像这样一个非常基本的版本:

import os
import subprocess
import time
path = r"C:\Users\Luke\Desk到p\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")

What this code is supposed 到 do is run my firefox/到r browser with the google url 5 times for 15 seconds each and then close it. It does just this, however, I can't seem 到 s到p the terminal from displaying the text: "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)

but when I do the program only opens the browser once, closes it and then ceases 到 open it again but still displays the "Times opened" text.

Anyone have any ideas as 到 how I can s到p the terminal from displaying this text?

谢谢

-卢克

answer1: 回答1:

Try this:

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

试试这个:

import os
import subprocess
import time
path = r"C:\Users\Luke\Desk到p\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 到 bind pipe 到 a name
    time.sleep(15)
    opened = opened + 1
    print ("Times opened:", opened)
    proc.terminate()                            # send process term signal
python  subprocess