使用psutil杀死每个python进程名称,除了一个

时间:2016-11-14 13:53:08

标签: python kill psutil

我正在使用以下python脚本来杀死具有给定名称的每个进程:

import psutil

for proc in psutil.process_iter():
    if proc.name() == "processname":
        proc.kill()

我希望脚本保留一个打开给定名称的进程。我怎样才能做到这一点?是否有可能使用这种方法?

2 个答案:

答案 0 :(得分:1)

你应该跳过第一个:

class Time {
    var timeString: String!
    var eventString: String!
}

class Date {
    var dateString: String!
    var times: [Time]!
}

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var dates = [Date]()

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = dates[indexPath.section].times[indexPath.row].timeString
        return cell
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return dates.count
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dates[section].times.count
    }

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let label = UILabel(frame: CGRectMake(0, 0, tableView.frame.width, 20))
        label.text = dates[section].dateString
        return label
    }
}

答案 1 :(得分:0)

这是另一个有效的解决方案:

[func() for func in [proc.kill for proc in psutil.process_iter() if proc.name()=="processname"][1:]]