确定Python中正在运行的程序

发布于 2024-09-13 15:50:24 字数 47 浏览 2 评论 0原文

我将如何使用 Python 来确定当前正在运行的程序。我在 Windows 上。

How would I use Python to determine what programs are currently running. I am on Windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剑心龙吟 2024-09-20 15:50:24

感谢 @hb2pencil 的 WMIC 命令!以下是如何在没有文件的情况下通过管道传输输出:

import subprocess
cmd = 'WMIC PROCESS get Caption,Commandline,Processid'
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
for line in proc.stdout:
    print line

Thanks to @hb2pencil for the WMIC command! Here's how you can pipe the output without a file:

import subprocess
cmd = 'WMIC PROCESS get Caption,Commandline,Processid'
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
for line in proc.stdout:
    print line
尴尬癌患者 2024-09-20 15:50:24
import os
os.system('WMIC /OUTPUT:C:\ProcessList.txt PROCESS get Caption,Commandline,Processid')
f = open("C:\ProcessList.txt")
plist = f.readlines()
f.close()

现在 plist 包含一个格式化的以空格分隔的进程列表:

  • 第一列是正在运行的可执行文件的名称
  • 第二列是表示正在运行的进程的命令
  • 第三列是进程 ID

这应该很容易用 python 解析。请注意,第一行数据是列的标签,而不是实际的过程。

注意这个方法只适用于windows!

import os
os.system('WMIC /OUTPUT:C:\ProcessList.txt PROCESS get Caption,Commandline,Processid')
f = open("C:\ProcessList.txt")
plist = f.readlines()
f.close()

Now plist contains a formatted whitespace-separated list of processes:

  • The first column is the name of the executable that is running
  • The second column is the command that represents the running process
  • The third column is the process ID

This should be simple to parse with python. Note that the first row of data are labels for the columns, and not actual processes.

Note that this method only works on windows!

小…楫夜泊 2024-09-20 15:50:24

与用于获取进程的实际 Python 工具相比,来自子进程命令的管道信息并不理想。尝试 psutil 模块。要获取进程号列表,请执行以下操作:

psutil.get_pid_list()

恐怕您必须在线下载此模块,它不包含在 python 发行版中,但这是解决您的问题的更好方法。要访问您拥有编号的进程的名称,请执行以下操作:

psutil.Process(<number>).name

这应该是您要查找的内容。另外,这里有一种查找特定进程是否正在运行的方法:

def process_exists(name):
    i = psutil.get_pid_list()
    for a in i:
        try:
            if str(psutil.Process(a).name) == name:
                return True
        except:
            pass
    return False

这使用可执行文件的名称,因此,例如,要查找 powershell 窗口,您可以这样做:

process_exists("powershell.exe")

Piping information from sub process commands is not ideal compared to an actual python tool meant for getting processes. Try the psutil module. To get a list of process numbers, do:

psutil.get_pid_list()

I'm afraid you have to download this module online, it is not included in python distributions, but this is a better way to solve your problem. To access the name of the process you have a number for, do:

psutil.Process(<number>).name

This should be what you are looking for. Also, here is a way to find if a specific process is running:

def process_exists(name):
    i = psutil.get_pid_list()
    for a in i:
        try:
            if str(psutil.Process(a).name) == name:
                return True
        except:
            pass
    return False

This uses the name of the executable file, so for example, to find a powershell window, you would do this:

process_exists("powershell.exe")
疧_╮線 2024-09-20 15:50:24

我的 get_pid_list() 访问被拒绝。一种较新的方法在 Windows 和 OSX 上对我有用:

import psutil

for proc in psutil.process_iter():
    try:
        if proc.name() == u"chrome.exe":
            print(proc)
            print proc.cmdline()
    except psutil.AccessDenied:
        print "Permission error or access denied on process"

I was getting access denied with get_pid_list(). A newer method worked for me on windows and OSX:

import psutil

for proc in psutil.process_iter():
    try:
        if proc.name() == u"chrome.exe":
            print(proc)
            print proc.cmdline()
    except psutil.AccessDenied:
        print "Permission error or access denied on process"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文