I l@ve RuBoard |
12.4 The grp Module(Unix only) The grp module provides an interface to the Unix group database (/etc/group). The getgrgid function returns data for a given group identity (see Example 12-4), and getgrnam returns data for a group name. Example 12-4. Using the grp ModuleFile: grp-example-1.py import grp import os print grp.getgrgid(os.getgid()) print grp.getgrnam("wheel") ('effbot', '', 4711, ['effbot']) ('wheel', '', 10, ['root', 'effbot', 'gorbot', 'timbot']) The getgrall function returns a list of database entries for all available groups. If you're going to do a lot of group queries, you can save some time by using getgrall to copy all the (current) groups into a dictionary. The groupinfo function in Example 12-5 returns the information for either a group identifier (an integer) or a group name (a string). Example 12-5. Using the grp Module to Cache Group InformationFile: grp-example-2.py import grp import os # preload password dictionary _grp = {} for info in grp.getgrall(): _grp[info[0]] = _grp[info[2]] = info def groupinfo(gid): # name or gid integer return _grp[gid] print groupinfo(os.getgid()) print groupinfo("wheel") ('effbot', '', 4711, ['effbot']) ('wheel', '', 10, ['root', 'effbot', 'gorbot', 'timbot']) |
I l@ve RuBoard |