Easy Tutorial
❮ Python Check Element Exists In List Python Prime Number ❯

Python3 os.chown() Method

Python3 OS File/Directory Methods


Overview

The os.chown() method is used to change the owner of a file. If no change is needed, you can set it to -1. You need superuser privileges to perform the permission modification operation.

This method is only supported on Unix.

Syntax

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

os.chown(path, uid, gid);

Parameters

-

path -- The file path for which permissions are to be set.

-

uid -- The user ID of the owner.

-

gid -- The group ID of the owner.

Return Value

This method does not return any value.

Example

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

#!/usr/bin/python3

import os, sys

# Assuming /tmp/foo.txt file exists.
# Set owner ID to 100
os.chown("/tmp/foo.txt", 100, -1)

print("Permissions modified successfully!!")

Executing the above program will output:

Permissions modified successfully!!

Python3 OS File/Directory Methods

❮ Python Check Element Exists In List Python Prime Number ❯