Python3 os.fchown() Method

Python3 OS File/Directory Methods


Overview

The os.fchown() method is used to change the ownership of a file. This function modifies the user ID and user group ID of a file specified by the file descriptor fd.

Available on Unix.

Syntax

fchown()The method syntax is as follows:

os.fchown(fd, uid, gid)

Parameters

  • fd-- file descriptor

  • uid-- user id of the file owner

  • gid-- user group id of the file owner

Return Value

This method has no return value.

Example

The following example demonstrates the use of the fchown() method:

Example (Python 3.0+)

#!/usr/bin/python3 import os, sys, stat # Open file "/tmp/foo.txt" fd = os.open( "/tmp", os.O_RDONLY ) # Set the user id of the file to 100 os.fchown( fd, 100, -1) # Set the user group id of the file to 50 os.fchown( fd, -1, 50) print ("Successfully changed permissions!!") # Close the file os.close( fd )

The output of the above program is:

Permissions modified successfully!!

Python3 OS File/Directory Methods

Other Extensions