将输出保存到数组,打印第一个元素

时间:2014-05-07 14:25:16

标签: python arrays python-2.7 append

我正在尝试将输出保存到数组,并打印第一个元素,但没有显示任何内容

logins = []

for us in c.Win32_LogonSession():
    try:
        for user in us.references("Win32_LoggedOnUser"):
            logins.append(user.Antecedent.Domain, user.Antecedent.Name, sep="\\")
            print(logins[0])
    except:
        pass

如何解决这个问题?

修改

没有例外。如果我重新编写代码

for us in c.Win32_LogonSession():
    try:
        for user in us.references("Win32_LoggedOnUser"):
            print(user.Antecedent.Domain, user.Antecedent.Name, sep="\\")
    except:
        pass

列出所有用户

DOMAIN\Glowie
DOMAIN\service_account
DOMAIN\service_account
DOMAIN\service_account
DOMAIN\service_account

1 个答案:

答案 0 :(得分:0)

这是最终的工作代码

user_list = []
for us in c.Win32_LogonSession():
    try:
        for user in us.references("Win32_LoggedOnUser"):
            #print(user.Antecedent.Domain, user.Antecedent.Name, sep="\\")
            user_logins = user.Antecedent.Domain + "\\" + user.Antecedent.Name
            user_list.append(user_logins)

    except:
        pass


userID = user_list[0]
print(userID)