【发布时间】:2011-05-17 05:55:01
【问题描述】:
我正在编写一个需要能够杀死某些进程的程序。我目前正在使用的两条线路;但是,第二行 os.system(task) 在结束进程时会在瞬间启动命令提示符。是否有任何不启动命令提示符的等效行?
片段:
task = 'taskkill /im ' + taskname + ' /f'
os.system(task)
如果您无法猜到,这是在 Windows 7 中。
【问题讨论】:
我正在编写一个需要能够杀死某些进程的程序。我目前正在使用的两条线路;但是,第二行 os.system(task) 在结束进程时会在瞬间启动命令提示符。是否有任何不启动命令提示符的等效行?
片段:
task = 'taskkill /im ' + taskname + ' /f'
os.system(task)
如果您无法猜到,这是在 Windows 7 中。
【问题讨论】:
尝试使用subprocess.check_call 而不是os.system。这不会在控制台窗口中启动进程。
import subprocess
taskname = '...'
task = 'taskkill /im ' + taskname + ' /f'
subprocess.check_call(task, shell=True)
【讨论】:
check_call 只是 call 的包装,它本身就是 Popen 的包装