Python| os.getgid() 和 os.setgid() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
os 模块中的所有函数在文件名和路径无效或不可访问的情况下,或具有正确类型但操作系统不接受的其他参数的情况下引发OSError 。
Python中的os.getgid()
方法用于获取当前进程的真实组id, os.setgid()
方法用于设置当前进程的真实组id。
注意: os.setgid()
和os.getgid()
方法仅在 UNIX 平台上可用。
os.getgid() 方法
Syntax: os.getgid()
Parameter: No parameter is required
Return Type: This method returns an integer value which represents the current process’s real group id.
代码 #1:使用 os.getgid() 方法
# Python program to explain os.getgid() method
# importing os module
import os
# Get the group id
# of the current process
# using os.getgid() method
gid = os.getgid()
# Print the group ID
# of the current process
print("Group id of the current process:", gid)
输出:
Group id of the current process: 1000
os.setgid() 方法
Syntax: os.setgid(euid)
Parameter:
euid: An integer value representing new group id for the current process.
Return Type: This method does not return any value.
代码 #2:使用 os.setgid() 方法
# Python program to explain os.setgid() method
# importing os module
import os
# Get the group id
# of the current process
# using os.getgid() method
gid = os.getgid()
# Print the real group id
# of the current process
print("Group id of the current process:", gid)
# Change the group id
# of the current process
# using os.setgid() method
gid = 23
os.setgid(gid)
print("Group id changed")
# Print the group id
# of the current process
gid = os.getgid()
print("Group id of the current process:", gid)
输出:
Group id of the current process: 0
Group id changed
Group id of the current process: 23