如何解决Python脚本记录Windows上当前正在运行的进程的路径
我想记录特定时间在Windows系统上运行的进程,并过滤掉所有主要进程(即排除所有后台进程),然后将它们的路径保存到文件中,我希望将其用于单独的脚本可以在需要时运行所有这些进程。 我使用wmi模块的方法来获取正在运行的进程,但是我想过滤掉主要的进程(例如Chrome,VS Code,Photoshop,Ms Office等)。因此,请帮助我,并提出我应该继续的建议。 以下是我想出的代码,但这并不总是有效,而且我也不知道如何获取正在运行的进程的可执行文件的路径,因此我可以使用subprocess.Popen()在另一个脚本中触发它们。 )。
import wmi
# Initializing the wmi constructor
f = wmi.WMI()
pre = dict()
# Printing the header for the later columns
print("pid Process name")
# Iterating through all the running processes when no app is opened
for process in f.Win32_Process():
# Displaying the P_ID and P_Name of the process
print(f"{process.ProcessId:<10} {process.Name}")
pre[process.ProcessId] = process.Name
wait = input("Go and run applications and then press a key: ")
post = dict()
# Iterating through all the running processes after opening the apps we want to record
for process in f.Win32_Process():
# Displaying the P_ID and P_Name of the process
print(f"{process.ProcessId:<10} {process.Name}")
post[process.ProcessId] = process.Name
#printing all the unique processes
print("\nUnique processes: ")
for i in post:
if i not in pre:
print(i,"\t",post[i])
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。