Python os.getgrouplist() - 获取指定用户所属的所有组id的列表

Python os.getgrouplist()

Python os模块中的所有函数在文件名和路径无效或不可访问,或其他具有正确类型但操作系统不接受的参数时都会引发OSError。

在类unix系统中,可以将多个用户放到一个组中。组标识符(通常缩写为GID)是一个用于表示特定组的数值。它将一个系统用户与其他共享相同内容的用户关联起来。

Python中的os.getgrouplist()方法用于获取指定用户所属的所有组id的列表

注意:os.getgrouplist()方法只在UNIX平台上可用。

语法:os.getgrouplist(user, gid)

参数:

user:表示系统用户的字符串值。

gid:整型值,表示组id。

如果gid不属于指定的用户,它也将包含在返回列表中

返回类型:该方法返回一个列表,其中表示指定用户所属的所有组id。

示例1

使用os.getgrouplist()方法

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

Python os.getgrouplist()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程