getgrouplist() ?
Douglas Zongker
dougz at cs.washington.edu
Fri Jun 14 22:09:27 EDT 2002
Graham Guttocks <graham_guttocks at yahoo.co.nz> wrote:
: I need a way to get a back a list of supplemental group ids for a
: specified uid. Something like getgrouplist() in my C standard lib. I
: don't see a convenient method for this, have I missed it?
Use the "grp" and "pwd" modules. I didn't see a convenient way to do
exactly what you want; I had to iterate over the list of all groups to
find the ones with a specific user:
>>> import grp
>>> def groups(uname):
... return [i[0] for i in grp.getgrall() if uname in i[-1]]
...
>>> groups('root')
['root', 'bin', 'daemon', 'sys', 'adm', 'disk', 'wheel']
>>>
The 'pwd' module can be used to translate between username and numeric
uid.
dz
More information about the Python-list
mailing list