【问题标题】:check if any item exist in the list (python)检查列表中是否存在任何项目(python)
【发布时间】:2018-10-07 08:43:14
【问题描述】:

我编写了一个工具来获取 Windows 进程列表...然后搜索任何 Internet 浏览器(进程名称),例如 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()

我杀死了所有浏览器进程并运行了它应该给我的代码,但是……他保持循环并运行这段代码:

 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()

这是我的输出:

C:\Program Files (x86)\Google\Chrome\Application\chrome.exe C:\Program 文件 (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

【问题讨论】:

  • 你能指定什么不起作用吗?请给出一个示例输出及其(错误)输出,以及所需的输出。
  • 我修改它.....

标签: python list loops process wmic


【解决方案1】:

if any(item in q for item in d) :没有做你认为的事情。

if 命令的问题在于您首先有一个for 循环。 q 是您要检查的单个字符串,因此 item in q 实际上是在检查字符串(作为对象字符串)是否在 q 字符串的元素之一内(因此在其他),这是行不通的。我建议拆分每个测试字符串以保留文件名。

那么for item in d 也没有按照你的想法做。它也将一个字符一个接一个地循环到字符串中。您可能希望先拆分字符串,因此最终结果可能类似于:

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) :

可能并不完美,但这是一个开始。

【讨论】:

  • 不工作 :( 它只是在我的浏览器运行时打印任何发现的内容
  • 什么不起作用?这是一个开始,您仍然需要检查每个中间步骤是否完成了它应该做的事情,我没有调试它,我的盒子上没有 cygwin 或 mays。
【解决方案2】:

您可能希望将 os.popen(...).read() 的结果拆分为像 'os.popen(...).read().split('\n') 这样的换行符

这样你就有了一个字符串列表,而不是一个长字符串。

我也改变了你的 if 逻辑。将来使用比“d”和“q”更具描述性的变量名。希望这会有所帮助!

我没有窗户,所以我没有测试过。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-21
    • 2021-06-25
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2013-12-12
    • 1970-01-01
    相关资源
    最近更新 更多