How to get the user/group name from uid/gid in python ?

raocheng rc.china at gmail.com
Sat Jan 19 20:43:49 EST 2008


We know that we can get process or file's uid/gid in python. For
example:
$ python
>>> import os
>>> os.getuid()
1516
>>> os.getgid()
102
>>> os.geteuid()
1516
>>> os.getegid()
102
>>> os.getgroups()
[102, 600]
>>>


$ python
>>> import os,stat
>>> uid = os.stat("./file1")[stat.ST_UID]
>>> gid = os.stat("./file1")[stat.ST_GID]
>>> print uid, gid
1516 102
>>>

How can we get the user/group name from the uid/gid ?
Generally, we can get this information from /etc/passwd. But it is too
inconvenient and when we are using integrated password management, we
can not depend on /etc/passwd.

Thanks in advance.



More information about the Python-list mailing list