简体   繁体   English

使用Python结束外部程序

[英]Ending external programs with Python

I'm writing a program that needs to be able to kill certain processes. 我正在编写一个程序,该程序必须能够终止某些进程。 The two lines I'm currently using work; 我当前正在使用的两行代码; however, the second line os.system(task) launches command prompt for a split second whilst it's ending a process. 但是,第二行os.system(task)在结束进程时会瞬间启动命令提示符。 Are there any equivalent lines which do not launch command prompt? 是否有不启动命令提示符的等效行?

Snippet : 片段:

task = 'taskkill /im ' + taskname + ' /f'
os.system(task)

This is in Windows 7 if you couldn't have guessed. 如果您猜不到的话,这就是Windows 7。

Try using subprocess.check_call instead of os.system . 尝试使用subprocess.check_call ,而不是os.system This won't launch the process in a console window. 这不会在控制台窗口中启动该过程。

import subprocess
taskname = '...'
task = 'taskkill /im ' + taskname + ' /f'
subprocess.check_call(task, shell=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM