列表未按预期工作

2024-09-27 21:33:54 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的列表建议只有满足domainuserl条件的用户才应该添加到users()。但是我在sorted_list中得到了空结果。有人能告诉我为什么吗?你知道吗

domain = 'domainanme'
user_list = [test1, test2, test3]
new_list = []

for userl in user_list:
    try:
        out = subprocess.check_output(["tasklist", "/V", "/FO", "List", "/FI", "USERNAME eq {0}\{1}" .format(domain, userl)], stderr=subprocess.STDOUT)
        users = [item for item in out.split() if domain in item and userl in item]
        sorted_list = set(users)
        print sorted_list
        if sorted_list != None:   # this was an attempted to remove the EMPTY items
            for name in sorted_list:
                print name
                new_list.append(name)
            else:
                pass

打印姓名输出

set([])
set([])
set([])

输出如下:

tasklist output


Tags: nameinnewforifdomainoutitem
1条回答
网友
1楼 · 发布于 2024-09-27 21:33:54

输出中的域名是大写的;请确保考虑到这一点。规范化both的大小写以确保不区分大小写的匹配:

users = [item for item in out.split() if domain.upper() in item.upper() and userl in item]

我会更智能地解析输出,因为上面的内容很容易导致误报(进程名中包含域和用户名,即使是重叠的文本,也会匹配)。你知道吗

相关问题 更多 >

    热门问题