列出用户和组的Python脚本 [英] Python script to list users and groups

查看:80
本文介绍了列出用户和组的Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本可以像这样在每个行上输出每个用户及其组:

I'm attempting to code a script that outputs each user and their group on their own line like so:

user1 group1  
user2 group1  
user3 group2  
...  
user10 group6

我为此用python编写了一个脚本,但想知道SO如何做到这一点.

I'm writing up a script in python for this but was wondering how SO might do this.

p.s.用任何语言都可以,但我更喜欢python.

p.s. Take a whack at it in any language but I'd prefer python.

我正在Linux上工作. Ubuntu 8.10或CentOS =)

I'm working on Linux. Ubuntu 8.10 or CentOS =)

推荐答案

对于* nix,您具有 grp 模块.您遍历pwd.getpwall()以获取所有用户.您可以使用grp.getgrgid(gid)查找他们的组名.

For *nix, you have the pwd and grp modules. You iterate through pwd.getpwall() to get all users. You look up their group names with grp.getgrgid(gid).

import pwd, grp
for p in pwd.getpwall():
    print p[0], grp.getgrgid(p[3])[0]

这篇关于列出用户和组的Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆