Open In App

Python | os.lchown() method

Last Updated : 11 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




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


Output:
os.lchown() mwthod terminal output

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




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


Output:
os.lchown() method terminal output

Code #3: If the specified path is a symlink




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


Output:
os.lchown() method terminal output

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads