Python 3 – os.chown() Method

Python 3 – os.chown() Method

Description

The chown() method changes the owner and group IDs of a path to numeric UIDs and GIDs. To keep one of the IDs, set it to -1. To set ownership, you need superuser privileges.

Syntax

Below is the syntax of the chown() method –

os.chown(path, uid, gid)

Parameters

  • path – The path whose owner and group IDs need to be set.
  • uid – The owner ID of the file.

  • gid – The group ID of the file.

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

# Assume /tmp/foo.txt exists.

# To set owner ID 100, you must do the following.
os.chown("/tmp/foo.txt", 100, -1)

print ("Operation successful!")

Result

When you run the above program, you will get the following output –

Operation successful!

Leave a Reply

Your email address will not be published. Required fields are marked *