grp module
in Python provides access to the
Unix group database. Each entry of
Unix group database is reported as a tuple-like object whose attributes are similar as the members of
group structure defined in
<grp.h> header.
Following are the attributes of the tuple-like object which represents the entries stored in
Unix group database:
Index |
Attributes |
Meaning |
0 |
gr_name |
the name of the group |
1 |
gr_passwd |
the (encrypted) group password; often empty |
2 |
gr_gid |
the numerical group ID |
3 |
gr_mem |
all the group member’s user names |
Note: grp module
is UNIX specific service. So, all methods of this module is available on UNIX versions only.
grp.getgrnam()
in Python defines following methods:
grp.getgrgid() method
grp.getgrnam() method
grp.getgrall() method
grp.getgrgid() method -
grp.getgrgid()
method in Python is used to get the entry stored in the UNIX group database for the specified group id.
KeyError exception is raised if the specified group id is invalid or the entry associated with it can not be found.
Syntax: grp.getgrgid(gid)
Parameter:
gid: An integer value representing the group id for which group database entry is required.
Return type: This method returns a tuple-like object of class 'grp.struct_group' which represents the group database entry associated with the specified group id (gid).
Code: Use of
grp.getgrgid()
method
Python3
# Python program to explain grp.getgrgid() method
# importing grp module
import grp
# Group id
gid = 1000
# Get the group
# database entry for the
# specified group id
# using grp.getgrgid() method
entry = grp.getgrgid(gid)
# Print the retrieved entry
print("Group database entry for group id % s:" % gid)
print(entry)
# Group id
gid = 0
# Get the group
# database entry for the
# specified group id
# using grp.getgrgid() method
entry = grp.getgrgid(gid)
# Print the retrieved entry
print("\nGroup database entry for group id % s:" % gid)
print(entry)
Output:
Group database entry for group id 1000:
grp.struct_group(gr_name='ihritik', gr_passwd='x', gr_gid=1000, gr_mem=[])
Group database entry for group id 0:
grp.struct_group(gr_name='root', gr_passwd='x', gr_gid=0, gr_mem=[])
grp.getgrnam() method -
grp.getgrnam()
method in Python is used to get the entry stored in UNIX group database for the specified group name.
KeyError exception is raised if the specified group name is invalid or the entry associated with it can not be found.
Syntax: grp.getgrnam(name)
Parameter:
name: A string value representing the group name for which group database entry is required.
Return type: This method returns a tuple-like object of class 'grp.struct_group' which represents the group database entry associated with the specified group name.
Code: Use of
grp.getgrnam()
method
Python3
# Python program to explain grp.getgrnam() method
# importing grp module
import grp
# Group name
name = "ihritik"
# Get the group
# database entry for the
# specified group name
# using grp.getgrnam() method
entry = grp.getgrnam(name)
# Print the retrieved entry
print("Group database entry for the group name '%s':" %name)
print(entry)
# Group name
name = "root"
# Get the group
# database entry for the
# specified group name
# using grp.getgrnam() method
entry = grp.getgrnam(name)
# Print the retrieved entry
print("\nGroup database entry for the group name '% s':" % name)
print(entry)
Output:
Group database entry for the group name 'ihritik':
grp.struct_group(gr_name='ihritik', gr_passwd='x', gr_gid=1000, gr_mem=[])
Group database entry for the group name 'root':
grp.struct_group(gr_name='root', gr_passwd='x', gr_gid=0, gr_mem=[])
grp.getgrall() method -
grp.getgrall()
method in Python is used to get the all available entry stored in UNIX group database.
Syntax: grp.getgrall()
Parameter: No parameter is required.
Return type: This method returns a list of tuple-like object of class 'grp.struct_group' whose elements represent group database entries.
Code: Use of
grp.getgrall()
method
Python3
# Python program to explain grp.getgrall() method
# importing grp module
import grp
# Get the all available group
# database entries
# using grp.getgrall() method
entries = grp.getgrall()
# Print the retrieved entry
print("Group database entries:")
for row in entries:
print(row)
Output:
Group database entries:
grp.struct_group(gr_name='root', gr_passwd='x', gr_gid=0, gr_mem=[])
grp.struct_group(gr_name='daemon', gr_passwd='x', gr_gid=1, gr_mem=[])
grp.struct_group(gr_name='bin', gr_passwd='x', gr_gid=2, gr_mem=[])
grp.struct_group(gr_name='sys', gr_passwd='x', gr_gid=3, gr_mem=[])
grp.struct_group(gr_name='adm', gr_passwd='x', gr_gid=4, gr_mem=['syslog', 'ihritik'])
grp.struct_group(gr_name='tty', gr_passwd='x', gr_gid=5, gr_mem=[])
grp.struct_group(gr_name='disk', gr_passwd='x', gr_gid=6, gr_mem=[])
grp.struct_group(gr_name='lp', gr_passwd='x', gr_gid=7, gr_mem=[])
grp.struct_group(gr_name='mail', gr_passwd='x', gr_gid=8, gr_mem=[])
grp.struct_group(gr_name='news', gr_passwd='x', gr_gid=9, gr_mem=[])
grp.struct_group(gr_name='uucp', gr_passwd='x', gr_gid=10, gr_mem=[])
grp.struct_group(gr_name='man', gr_passwd='x', gr_gid=12, gr_mem=[])
grp.struct_group(gr_name='proxy', gr_passwd='x', gr_gid=13, gr_mem=[])
grp.struct_group(gr_name='kmem', gr_passwd='x', gr_gid=15, gr_mem=[])
grp.struct_group(gr_name='dialout', gr_passwd='x', gr_gid=20, gr_mem=[])
grp.struct_group(gr_name='fax', gr_passwd='x', gr_gid=21, gr_mem=[])
grp.struct_group(gr_name='voice', gr_passwd='x', gr_gid=22, gr_mem=[])
grp.struct_group(gr_name='cdrom', gr_passwd='x', gr_gid=24, gr_mem=['ihritik'])
grp.struct_group(gr_name='floppy', gr_passwd='x', gr_gid=25, gr_mem=[])
grp.struct_group(gr_name='tape', gr_passwd='x', gr_gid=26, gr_mem=[])
grp.struct_group(gr_name='sudo', gr_passwd='x', gr_gid=27, gr_mem=['ihritik'])
grp.struct_group(gr_name='audio', gr_passwd='x', gr_gid=29, gr_mem=['pulse'])
grp.struct_group(gr_name='dip', gr_passwd='x', gr_gid=30, gr_mem=['ihritik'])
grp.struct_group(gr_name='www-data', gr_passwd='x', gr_gid=33, gr_mem=[])
grp.struct_group(gr_name='backup', gr_passwd='x', gr_gid=34, gr_mem=[])
grp.struct_group(gr_name='operator', gr_passwd='x', gr_gid=37, gr_mem=[])
grp.struct_group(gr_name='list', gr_passwd='x', gr_gid=38, gr_mem=[])
grp.struct_group(gr_name='irc', gr_passwd='x', gr_gid=39, gr_mem=[])
grp.struct_group(gr_name='src', gr_passwd='x', gr_gid=40, gr_mem=[])
grp.struct_group(gr_name='gnats', gr_passwd='x', gr_gid=41, gr_mem=[])
grp.struct_group(gr_name='shadow', gr_passwd='x', gr_gid=42, gr_mem=[])
grp.struct_group(gr_name='utmp', gr_passwd='x', gr_gid=43, gr_mem=[])
grp.struct_group(gr_name='video', gr_passwd='x', gr_gid=44, gr_mem=[])
grp.struct_group(gr_name='sasl', gr_passwd='x', gr_gid=45, gr_mem=[])
grp.struct_group(gr_name='plugdev', gr_passwd='x', gr_gid=46, gr_mem=['ihritik'])
grp.struct_group(gr_name='staff', gr_passwd='x', gr_gid=50, gr_mem=[])
grp.struct_group(gr_name='games', gr_passwd='x', gr_gid=60, gr_mem=[])
grp.struct_group(gr_name='users', gr_passwd='x', gr_gid=100, gr_mem=[])
grp.struct_group(gr_name='nogroup', gr_passwd='x', gr_gid=65534, gr_mem=[])
grp.struct_group(gr_name='systemd-journal', gr_passwd='x', gr_gid=101, gr_mem=[])
grp.struct_group(gr_name='systemd-timesync', gr_passwd='x', gr_gid=102, gr_mem=[])
grp.struct_group(gr_name='systemd-network', gr_passwd='x', gr_gid=103, gr_mem=[])
grp.struct_group(gr_name='systemd-resolve', gr_passwd='x', gr_gid=104, gr_mem=[])
grp.struct_group(gr_name='systemd-bus-proxy', gr_passwd='x', gr_gid=105, gr_mem=[])
grp.struct_group(gr_name='input', gr_passwd='x', gr_gid=106, gr_mem=[])
grp.struct_group(gr_name='crontab', gr_passwd='x', gr_gid=107, gr_mem=[])
grp.struct_group(gr_name='syslog', gr_passwd='x', gr_gid=108, gr_mem=[])
grp.struct_group(gr_name='messagebus', gr_passwd='x', gr_gid=109, gr_mem=[])
grp.struct_group(gr_name='netdev', gr_passwd='x', gr_gid=110, gr_mem=[])
grp.struct_group(gr_name='mlocate', gr_passwd='x', gr_gid=111, gr_mem=[])
grp.struct_group(gr_name='ssl-cert', gr_passwd='x', gr_gid=112, gr_mem=[])
grp.struct_group(gr_name='uuidd', gr_passwd='x', gr_gid=113, gr_mem=[])
grp.struct_group(gr_name='rtkit', gr_passwd='x', gr_gid=114, gr_mem=[])
grp.struct_group(gr_name='avahi-autoipd', gr_passwd='x', gr_gid=115, gr_mem=[])
grp.struct_group(gr_name='bluetooth', gr_passwd='x', gr_gid=116, gr_mem=[])
grp.struct_group(gr_name='ssh', gr_passwd='x', gr_gid=117, gr_mem=[])
grp.struct_group(gr_name='lpadmin', gr_passwd='x', gr_gid=118, gr_mem=['ihritik'])
grp.struct_group(gr_name='whoopsie', gr_passwd='x', gr_gid=119, gr_mem=[])
grp.struct_group(gr_name='avahi', gr_passwd='x', gr_gid=120, gr_mem=[])
grp.struct_group(gr_name='scanner', gr_passwd='x', gr_gid=121, gr_mem=['saned'])
grp.struct_group(gr_name='saned', gr_passwd='x', gr_gid=122, gr_mem=[])
grp.struct_group(gr_name='pulse', gr_passwd='x', gr_gid=123, gr_mem=[])
grp.struct_group(gr_name='pulse-access', gr_passwd='x', gr_gid=124, gr_mem=[])
grp.struct_group(gr_name='colord', gr_passwd='x', gr_gid=125, gr_mem=[])
grp.struct_group(gr_name='geoclue', gr_passwd='x', gr_gid=126, gr_mem=[])
grp.struct_group(gr_name='gdm', gr_passwd='x', gr_gid=127, gr_mem=[])
grp.struct_group(gr_name='ihritik', gr_passwd='x', gr_gid=1000, gr_mem=[])
grp.struct_group(gr_name='sambashare', gr_passwd='x', gr_gid=128, gr_mem=['ihritik'])
grp.struct_group(gr_name='hadoop', gr_passwd='x', gr_gid=1001, gr_mem=['master'])
grp.struct_group(gr_name='master', gr_passwd='x', gr_gid=1002, gr_mem=[])
Similar Reads
Import module in Python In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
__future__ Module in Python __future__ module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions.. This module includes all the latest functions which were not present in the previous version in Python. And we can use this by importing the __future__ module. I
4 min read
Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Python Fire Module Python Fire is a library to create CLI applications. It can automatically generate command line Interfaces from any object in python. It is not limited to this, it is a good tool for debugging and development purposes. With the help of Fire, you can turn existing code into CLI. In this article, we w
3 min read
External Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m
5 min read
Inspect Module in Python The inspect module in Python is useful for examining objects in your code. Since Python is an object-oriented language, this module helps inspect modules, functions and other objects to better understand their structure. It also allows for detailed analysis of function calls and tracebacks, making d
4 min read
Python Modules Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work.In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we c
7 min read
Built-in Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext
9 min read
Python Math Module Math Module consists of mathematical functions and constants. It is a built-in module made for mathematical tasks. The math module provides the math functions to deal with basic operations such as addition(+), subtraction(-), multiplication(*), division(/), and advanced operations like trigonometric
13 min read
Platform Module in Python Platform module in Python is a built-in library that provides a portable way to access detailed information about the underlying platform (hardware and operating system) on which your Python program is running. This can include data such as the OS name and version, machine type, processor info and P
3 min read