Python | os.lchown() 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.lchown() method in Python is used to change the owner and group id of the specified file path to the specified numeric owner id (UID) and group id (GID). This method does not follow symlinks and is equivalent to os.chown(path, uid, gid, follow_symlinks = False) method.



Note: os.lchown() method is available only on UNIX platforms and the functionality of this method is typically available only to the superuser or a privileged user.

Syntax: os.lchown(pth, uid, gid)

Parameters:
path: A path-like object representing the he file path for which ownership to be set.
uid: An integer value representing the owner id to be set for the file.
gid: An integer value representing the group id to be set for the file.

Return Type: This method does not return any value.

Code #1: Use of os.lchown() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.lchown() method 
    
# importing os module 
import os
  
# File path
path = "./file.txt"
  
# Print the current owner id
# and group id of the file
  
# os.stat() method will return a 
# 'stat_result’ object of
# ‘os.stat_result’ class whose
# 'st_uid' and 'st_gid' attributes
# will represent owner id and group id
# of the file respectively 
print("Owner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid) 
  
  
# Change the owner id and 
# the group id of the file
# using os.lchown() method
uid = 400
gid = 500
os.lchown(path, uid, gid)
print("\nOwner and group id of the file changed")
  
   
# Print the owner id
# and group id of the file
print("\nOwner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid) 

chevron_right


Output:
os.lchown() mwthod terminal output

Code #2: Use of os.lchown() method to set any one id and leave other unchanged.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.lchown() method 
    
# importing os module 
import os
  
# File path
path = "./file.txt"
  
# Print the current owner id
# and group id of the file
  
# os.stat() method will return a 
# 'stat_result’ object of
# ‘os.stat_result’ class whose
# 'st_uid' and 'st_gid' attributes
# will represent owner id and group id
# of the file respectively 
print("Owner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid) 
  
  
# Change only group id of 
# the file and leave 
# owner id unchanged
  
# set id as -1 to leave
# it unchanged
uid = 1000
gid = -1
os.lchown(path, uid, gid)
print("\ngroup id of the file changed")
  
   
# Print the owner id
# and group id of the file
print("\nOwner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid) 

chevron_right


Output:
os.lchown() method terminal output

Code #3: If the specified path is a symlink

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.lchown() method 
    
# importing os module 
import os
  
# File path
path = "./file.txt"
  
  
# Creating a symlink
# of the above path  
# using os.symlink() method
symlink = "file(symlink).txt"
os.symlink(path, symlink)
   
# Print the current owner id
# and group id of the file
# as well as the symlink pointing
# to the above specified file path 
print("Owner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid) 
  
print("Owner id of the symlink:", os.stat(symlink).st_uid)
print("Group id of the symlink:", os.stat(symlink).st_gid) 
  
# Change the owenership 
# of the symlink pointing 
# to the above file 'file.txt'
uid = 600
gid = 700
os.lchown(symlink, uid, gid)
print("\nOwner id and group id changed")
  
  
# Print the owner id
# and group id of the file
# as well as the symlink pointing
# to the above specified file path
print("\nOwner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid) 
  
print("Owner id of the symlink:", os.stat(symlink).st_uid)
print("Group id of the symlink:", os.stat(symlink).st_gid)
  
  
# As os.lchown() method
# does not follow symlinks
# so, we can not change the
# owner and group id 
# through a symlink 

chevron_right


Output:
os.lchown() method terminal output

Reference: https://docs.python.org/3/library/os.html



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.