Easy Tutorial
❮ Python Os Mkfifo Python Five Fish ❯

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 group ID of a file specified by the file descriptor fd.

Available on Unix.

Syntax

The syntax for the fchown() method is as follows:

os.fchown(fd, uid, gid)

Parameters

-

fd -- File descriptor

-

uid -- User ID of the file owner

-

gid -- Group ID of the file owner

Return Value

This method does not return any value.

Example

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

Example (Python 3.0+)

#!/usr/bin/python3

import os, sys, stat

# Open the 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 group ID of the file to 50
os.fchown(fd, -1, 50)

print("Permissions modified successfully!!")

# Close the file
os.close(fd)

Executing the above program outputs:

Permissions modified successfully!!

Python3 OS File/Directory Methods

❮ Python Os Mkfifo Python Five Fish ❯