I am trying to use psutil library to get users logged in to linux system.
For that i used function psutil.users()
>>> import psutil
>>> psutil.users()
[suser(name='vibhcool', terminal='tty2',host='localhost',started=1473815296.0)]
I want to extract the username from this list, what shall i do? Also what is suser here?
I don't know why they choose the name suser
, but it's actually a namedtuple.
That shouldn't matter, you get the name of a user like so:
>>> import psutil
>>> users = psutil.users()
>>> first_user = users[0]
>>> name = first_user.name
>>> print(name)
'vibhcool'
In short:
>>> import psutil
>>> print(psutil.users()[0].name)
'vibhcool'
I got the answer, (sorry i am bad at googling)
psutil.users() outputs a list, so it can be traversed using for loop
users = psutil.users()
for user in users:
print(user.name)
reference: http://www.programcreek.com/python/example/53877/psutil.users
这样做不是很安全,而是:
users_name = [user[0] for user in psutil.users()]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.