grp module in Python

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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=[])



My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.