【问题标题】:Choose specific user from list using `os.system()`使用 os.system() 从列表中选择特定用户
【发布时间】:2016-01-31 15:30:25
【问题描述】:

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

到目前为止我有:

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

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

【问题讨论】:

  • 这取决于你使用的是什么。
  • 你的意思是,你想得到os.system()的输出吗?如果是这样,this question 会很有帮助。
  • 我的意思是说我拥有的用户列表是 root、user1、user2 等。在我选择之后让我们说 user2 需要弹出一个新的选项列表,其中包含新的命令,例如 show user组或显示用户 ID 等...
  • 为什么不直接在 Python 中解析 /etc/passwd

标签: python list debian


【解决方案1】:

您可以使用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

【讨论】:

  • 感谢解决了我一半的问题,现在我只需要弄清楚如何告诉程序打开与我选择的用户相关的另一个列表。比如:1)显示用户组2)显示用户ID 3)显示用户别名等等
猜你喜欢
  • 2012-04-05
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多