使用“os.system()”从列表中选择特定用户

Choose specific user from list using `os.system()`

我需要在系统中的所有用户Python中创建一个菜单屏幕,系统显示列表后你选择你想要的用户然后弹出另一个菜单。

到目前为止我有:

os.system("cut -d: -f1 /etc/passwd")    
chose = str(raw_input("Select user from this list > "))

如何在选择用户后显示第二个列表?

您可以使用 pwdgrp 模块。
我们使用grp获取用户组。

import pwd, grp

user = ''
users = pwd.getpwall()
i=0
for p in users:
    print str(i) + ')' + str(p[0])
    i+=1


chose = int(raw_input("Select user from this list > "))
user = users[chose]
print
print '1) show user groups \n2) show user id'
print 
chose = int(raw_input("Your choice > "))

if chose == 1:
    print grp.getgrgid(user.pw_gid).gr_name
else:
    print user.pw_uid