Python | os.fchown() 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.fchown()
method in Python is used to change the owner and group id of the file associated with the specified file descriptor to the specified numeric owner id (UID) and group id (GID). This method is equivalent to os.chown(fd, uid, gid)
method.
Note: os.fchown()
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.fchown(fd, uid, gid)
Parameters:
fd: A file descriptor representing the file whose uid and gid is 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. To leave any one of the ids unchanged, set it to -1.
Return Type: This method does not return any value.
Code #1: Use of os.fchown() method
import os
filename = "file.txt"
fd = os. open (filename, os.O_RDWR)
print ( "Owner id of the file:" , os.stat(fd).st_uid)
print ( "Group id of the file:" , os.stat(fd).st_gid)
uid = 200
gid = 300
os.fchown(fd, uid, gid)
print ( "\nOwner and group id of the file changed" )
print ( "\nOwner id of the file:" , os.stat(fd).st_uid)
print ( "Group id of the file:" , os.stat(fd).st_gid)
|
Output:
Code #2: Use of os.fchown() method to set any one id and leave other unchanged
import os
filename = "file.txt"
fd = os. open (filename, os.O_RDWR)
print ( "Owner id of the file:" , os.stat(fd).st_uid)
print ( "Group id of the file:" , os.stat(fd).st_gid)
uid = - 1
gid = 1000
os.fchown(fd, uid, gid)
print ( "\ngroup id of the file changed" )
print ( "\nOwner id of the file:" , os.stat(fd).st_uid)
print ( "Group id of the file:" , os.stat(fd).st_gid)
|
Output:
Code #3: If the specified path is a symlink
import os
path = "./file.txt"
symlink = "./file(symlink).txt"
os.symlink(path, symlink)
fd = os. open (symlink, os.O_RDWR)
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)
uid = 600
gid = 700
os.fchown(fd, uid, gid)
print ( "\nOwner id and group id changed" )
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)
|
Output:
Reference: https://docs.python.org/3/library/os.html
Last Updated :
11 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...