Python os.lchown() Method
os.lchown() Overview
The os.lchown() method is used to change the owner of a file, similar to chown, but without tracking links.
It is only supported for use under Unix.
os.lchown() Syntax
The syntax format of the lchown() method is as follows.
os.lchown(path, uid, gid)
os.lchown() Parameters
- path – the path of the file to set permissions
- uid – the user ID of the user
- gid – the group ID of the user
os.lchown() Return Value
This method has no return value.
os.lchown() Demo#1
The following example demonstrates the use of the lchown() method.
# Python program to explain os.lchown() method
# importing os module
import os
# File path
path = "/root/apidemos.com.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)
Outpu:
os.lchown() Demo#2
Use of os.lchown() method to set any one id and leave other unchanged.
# Python program to explain os.lchown() method in apidemos.com
# importing os module
import os
# File path
path = "/root/apidemos.com.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() Demo#3
If the specified path is a symlink
# Python program to explain os.lchown() method
# importing os module
import os
# File path
path = "/root/apidemos.com.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: