[英]check if any item exist in the list (python)
im coding a tool to get the list of windows process ... and then search for any internet browser (process name) like chrome.exe , opera.exe .... etc if found one of them then do something ... if didn't just print nothing found my code :我正在编写一个工具来获取 Windows 进程列表......然后搜索任何互联网浏览器(进程名称),如 chrome.exe 、opera.exe .... 等如果找到其中一个然后做一些事情......如果不只是打印没有找到我的代码:
import os
import time
def qa():
home = os.environ.get("HOMEDRIVE")
lol =home +"/"
mycurrent = os.getcwd()
change = os.chdir(lol)
mycurrent = os.getcwd()
d = os.popen("""wmic process get name | findstr /v "Name 'System Idle Process' System""").read().lower()
lists = ["opera.exe" , "chrome.exe" , "iexplore.exe" , "firefox.exe" , "microsoftedgecp.exe"]
for q in lists:
if any(item in q for item in d) :
qassam = os.popen("dir /s /b {}".format(lists[1])).read().strip()
print qassam
dir1 = os.path.dirname(qassam)
print dir1
#CreateShortCut_to_Desktop(qassam , dir1)
print time.time()
else:
print "nothing found"
break
qa()
i killed all the browser process and i run the code it should gives me nothing found but ... he keep loop and run this block of code :我杀死了所有浏览器进程并运行了代码它应该没有发现任何东西但是......他保持循环并运行这个代码块:
qassam = os.popen("dir /s /b {}".format(lists[1])).read().strip()
print qassam
dir1 = os.path.dirname(qassam)
print dir1
#CreateShortCut_to_Desktop(qassam , dir1)
print time.time()
and this is my output :这是我的输出:
C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe C:\\Program Files (x86)\\Google\\Chrome\\Application 1538829033.16 C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe C:\\Program Files (x86)\\Google\\Chrome\\Application 1538829041.34 C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe C:\\Program Files (x86)\\Google\\Chrome\\Application 1538829048.94 C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe C:\\Program Files (x86)\\Google\\Chrome\\Application
C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe C:\\Program Files (x86)\\Google\\Chrome\\Application 1538829033.16 C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome。 exe C:\\Program Files (x86)\\Google\\Chrome\\Application 1538829041.34 C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe C:\\Program Files (x86)\\Google\\Chrome\\Application 1538829048.94 C :\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe C:\\Program Files (x86)\\Google\\Chrome\\Application
if any(item in q for item in d) :
is not doing what you think it does. if any(item in q for item in d) :
没有做你认为它做的事情。
The issue with your if
command is that you have a for
loop first.您的
if
命令的问题在于您首先有一个for
循环。 The q
are the individual strings that you want to check against, so item in q
is actually checking that the string (as the object string) is inside one of the element of the q
string (so checking one element after the other), which cannot work. q
是您要检查的单个字符串,因此item in q
实际上是检查字符串(作为对象字符串)是否在q
字符串的元素之一内(因此检查一个接一个的元素),其中不能工作。 I would suggest to split each tested string to keep the filename.我建议拆分每个测试的字符串以保留文件名。
Then for item in d
is not doing what you think either.然后
for item in d
也没有按照您的想法行事。 It loops one character after the other int he string as well.它也在另一个 int 字符串之后循环一个字符。 You may want to split the string first, so the end result could be something like:
您可能想先拆分字符串,因此最终结果可能类似于:
d = os.popen("""wmic process get name | findstr /v "Name 'System Idle Process' System""").read().lower().split()
lists = ["opera.exe" , "chrome.exe" , "iexplore.exe" , "firefox.exe" , "microsoftedgecp.exe"]
if any(item.split(os.sep)[-1] in lists for item in d) :
May not be perfect, but it's a start.可能不完美,但这是一个开始。
You may want to split the result of os.popen(...).read()
on newlines like 'os.popen(...).read().split('\\n')您可能希望在换行符上拆分
os.popen(...).read()
的结果,例如 'os.popen(...).read().split('\\n')
That way you have a list of string, instead of one long string.这样你就有了一个字符串列表,而不是一个长字符串。
I also changed your if logic.我也改变了你的 if 逻辑。 In the future use more descriptive variable names than 'd' and 'q'.
将来使用比 'd' 和 'q' 更具描述性的变量名称。 Hope this helps!
希望这可以帮助!
I don't have windows, so I haven't tested this.我没有窗户,所以我没有测试过。
import os
import time
def qa():
home = os.environ.get("HOMEDRIVE")
lol =home +"/"
mycurrent = os.getcwd()
change = os.chdir(lol)
mycurrent = os.getcwd()
d = os.popen("""wmic process get name | findstr /v "Name 'System Idle Process' System""").read().lower().split('\n')
print('proc count',len(d))
lists = ["opera.exe" , "chrome.exe" , "iexplore.exe" , "firefox.exe" , "microsoftedgecp.exe"]
for q in lists:
#if any(item in q for item in d) :
if q in d:
print('proc',d,'browser',q)
qassam = os.popen("dir /s /b {}".format(lists[1])).read().strip()
print qassam
dir1 = os.path.dirname(qassam)
print dir1
#CreateShortCut_to_Desktop(qassam , dir1)
print time.time()
else:
print "nothing found"
break
qa()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.