Python| os.getgrouplist() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
os 模块中的所有函数在文件名和路径无效或不可访问的情况下,或具有正确类型但操作系统不接受的其他参数的情况下引发OSError 。
在类 UNIX 系统中,可以将多个用户放入一个组中。组标识符,通常缩写为GID ,是用于表示特定组的数值。它将系统用户与其他共享共同点的用户联系起来。
Python中的os.getgrouplist()
方法用于获取指定用户所属的所有组 ID 的列表。
注意: os.getgrouplist()
方法仅适用于 UNIX 平台。
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.
# Python program to explain os.getgrouplist() method
# importing os module
import os
# System user
user = "ihritik"
# Group id
gid = 100
# Get the list of all
# group ids the specified user
# belongs to using
# os.getgrouplist() method
groupList = os.getgrouplist(user, gid)
# Print the list
print("% s is associated with the following group ids:" % user)
print(groupList, "\n")
# System user
user = "root"
# Group id
gid = 100
# Get the list of all
# group ids the specified user
# belongs to using
# os.getgrouplist() method
groupList = os.getgrouplist(user, gid)
# Print the list
print("%s is associated with the following group ids:" %user)
print(groupList)
# If the specified gid does not
# belongs to the specified user
# it will also be included in
# the list of groups
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]