Python | os.setgroups() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

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

os.setgroups() method in Python is used to set the list of supplemental group ids associated with the current process to specified list.



supplementary group IDs: In Unix systems, every user must be a member of at least one group called primary group. It is also possible for a user to be listed as member of additional groups in the relevant entries in the group database. The IDs of these additional groups are called supplementary group IDs

Note: os.setgroups() method is available only on UNIX systems and functionality of this method is typically available only to the superuser.
Superuser means a root user or an administrative user who has all the permissions to run or execute any program in the operating system.

Syntax: os.setgroups(groups)

Parameter:
groups: The list of supplemental group IDs to be set. Each element of the list must be an integer representing a group.

Return Type: This method does not return any value.

Code: Use of os.setgroups() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.setgroups() method 
  
# importing os module 
import os
  
# Get the list of supplemental
# group IDs associated with
# the current process 
# using os.getgroups() method
sgid = os.getgroups()
  
# Print the list
print("Supplemental group IDs associated with the current process:")
print(sgid)
  
new_sgid = [ 20, 30, 40, 50]
  
# Set the list of supplemental 
# group IDs for the current process
# using os.setgroups() method 
os.setgroups(new_sgid) 
  
  
# Get the list of supplemental
# group IDs associated with
# the current process
sgid = os.getgroups()
  
# Print the list
print("New supplemental group IDs associated with the current process:")
print(sgid)

chevron_right


Output:

Supplemental group IDs associated with the current process:
[0]
New supplemental group IDs associated with the current process:
[20, 30, 40, 50]

Terminal-example



My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.