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 Module

File: 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 Information

File: 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'])

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols

Internationalization

Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules





Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net