确定 Python 中正在运行的程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3429250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determining running programs in Python
提问by asdsaaa
How would I use Python to determine what programs are currently running. I am on Windows.
我将如何使用 Python 来确定当前正在运行哪些程序。我在 Windows 上。
回答by hb2pencil
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:
现在 plist 包含一个格式化的以空格分隔的进程列表:
- 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
- 第一列是正在运行的可执行文件的名称
- 第二列是代表正在运行的进程的命令
- 第三列是进程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.
这应该很容易用 python 解析。请注意,第一行数据是列的标签,而不是实际过程。
Note that this method only works on windows!
请注意,此方法仅适用于 Windows!
回答by Nick Perkins
Thanks to @hb2pencil for the WMIC command! Here's how you can pipe the output without a file:
感谢@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
回答by trevorKirkby
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:
与用于获取进程的实际 python 工具相比,来自子进程命令的管道信息并不理想。试试 psutil 模块。要获取进程编号列表,请执行以下操作:
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:
恐怕您必须在线下载此模块,它不包含在 python 发行版中,但这是解决问题的更好方法。要访问您有编号的进程的名称,请执行以下操作:
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:
这使用可执行文件的名称,例如,要查找 powershell 窗口,您可以这样做:
process_exists("powershell.exe")
回答by David Dehghan
I was getting access denied with get_pid_list(). A newer method worked for me on windows and OSX:
我被 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"

