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.
# Python program to explain os.fchown() method # importing os module import os # File filename = "file.txt" # Open the file and get the # file descriptor associated # with it using os.open() method fd = os. open (filename, os.O_RDWR) # 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(fd).st_uid) print ( "Group id of the file:" , os.stat(fd).st_gid) # Change the owner id and # the group id of the file # associated with the file descriptor # using os.fchown() method uid = 200 gid = 300 os.fchown(fd, 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(fd).st_uid) print ( "Group id of the file:" , os.stat(fd).st_gid) |
Output:
# Python program to explain os.fchown() method # importing os module import os # File filename = "file.txt" # Open the file and get the # file descriptor associated # with it using os.open() method fd = os. open (filename, os.O_RDWR) # 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(fd).st_uid) print ( "Group id of the file:" , os.stat(fd).st_gid) # Change only group id of # the file and leave owner id # unchanged # set id as -1 to leave # it unchanged uid = - 1 gid = 1000 os.fchown(fd, 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(fd).st_uid) print ( "Group id of the file:" , os.stat(fd).st_gid) |
Output:
# Python program to explain os.fchown() 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) # Open the symlink # and get the file descriptor # associated with it using # os.open() method fd = os. open (symlink, os.O_RDWR) # 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.fchown(fd, uid, gid) print ( "\nOwner id and group id changed" ) # Print the owner id # and group id of the file # as well as the symlink 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.fchown() method # follows symlink # so, we can change the # owner and group id # through a symlink |
Output:
Reference: https://docs.python.org/3/library/os.html
Contact Us