Python os.getgid()和os.setgid()
os模块中的所有函数在文件名和路径无效或不可访问,或其他具有正确类型但操作系统不接受的参数时都会引发OSError。
Python中的os.getgid()方法用于获取当前进程的实际组id,而os.setgid()方法用于设置当前进程的实际组id。
注意:os.setgid()和os.getgid()方法只在UNIX平台上可用。
os.getgid()方法
语法:os.getgid()
参数:不需要参数
返回类型:该方法返回一个整数值,表示当前进程的真实组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()方法
语法:os.setgid(euid)
参数:
euid:整数值,表示当前进程的新组id。
返回类型:此方法不返回任何值。
示例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