Python | os.getgrouplist () method

Python Methods and Functions

All functions in the os module raise OSError for invalid or unreachable file names and paths or other arguments that are of the correct type but are not accepted by the operating system.

B UNIX-like systems multiple users can be combined into a group. The Group ID , often abbreviated to GID , is a numeric value used to represent a specific group. It connects the system user with other users who share something in common.

os.getgrouplist() is used in Python to get a list of all group IDs the specified user belongs to.

Note: os.getgrouplist () only available on UNIX platforms.

Syntax: os.getgrouplist (user, gid)

Parameters:
user : A string value representing a system user.
gid : An integer value representing a group id.
If gid does not belong to the specified user, it will also be included in the return list

Return Type: This method returns a list which represents all group ids that the specified user belongs to.

Code: using the os.getgrouplist ( )

# Python program to explain the os.getgrouplist () method

 
# import of the os module

import os

 
# System user

user = "ihritik"

 
# Group ID

gid = 100

 
# Get a list of all
# group identifies the specified user
# belongs to use
# os.getgrouplist () method

groupList = os. getgrouplist (user, gid)

 
# Print list

print ( "% s is associated with the following group ids : " % user)

print (groupList, "" )

 

 
# System user

user = "root"

 
# Group ID

gid  = 100

 
# Get a list of all
# group identifies the specified user
# belongs to usage
# os.getgrouplist () method

groupList = os.getgrouplist (user, gid)

 
# Print list

print ( "% s is associated with the following group ids: " % user)

print (groupList)

 

 
# If the specified guide is not
# belongs to the specified user
# it will also be included in
# group list

Exit:

 ihritik is associated with the following group ids: [100, 4, 24, 27, 30, 46, 118, 128 ] root is associated with the following group ids: [100] 





Get Solution for free from DataCamp guru