如何用python判断是否有某个exe
时间: 2024-05-03 13:20:57 浏览: 15
文件在运行?
可以使用Python的psutil库来判断是否有某个exe文件在运行。以下是示例代码:
```python
import psutil
def check_process(exe_name):
for proc in psutil.process_iter():
try:
if exe_name.lower() in proc.name().lower():
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
```
其中,exe_name为要判断的exe文件名,不包含.exe后缀。如果返回True,则表示有该exe文件在运行,否则表示没有。
相关问题
如何用python判断是否存在某个exe文件,代码
可以使用Python的os模块中的path.exists()函数来判断是否存在某个exe文件。代码如下:
```python
import os
file_path = "C:\\path\\to\\exe\\file.exe"
if os.path.exists(file_path):
print("The exe file exists!")
else:
print("The exe file does not exist!")
```
python怎么检测进程是否在某个路径中?
在Python中,你可以使用psutil模块来检测特定进程的运行路径是否在某个路径中。
以下是一个示例代码,它将列举所有正在运行的进程,并检查它们的运行路径是否在指定的路径中:
```python
import psutil
target_path = "/home/user/example"
for proc in psutil.process_iter(['pid', 'name', 'exe']):
try:
proc_info = proc.as_dict(attrs=['pid', 'name', 'exe'])
exe_path = proc_info['exe']
if target_path in exe_path:
print(f"Process {proc_info['name']} with PID {proc_info['pid']} is running in {target_path}")
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
```
其中,`psutil.process_iter()`函数将返回一个迭代器,它可以枚举当前运行的所有进程。`proc.as_dict()`函数用于检索进程的PID、名称和可执行文件路径等信息。`psutil.NoSuchProcess`、`psutil.AccessDenied`和`psutil.ZombieProcess`是可能出现的异常,我们在代码中使用`try-except`代码块来捕获异常并忽略它们。
在代码中,我们将目标路径指定为`target_path`变量,然后在`if`语句中检查进程的可执行文件路径是否包含该路径,如果是,则将进程的名称和PID输出到控制台。