Python3 os.chown() Method

Python3 OS File/Directory Methods


Overview

The os.chown() method is used to change the file owner. If you do not want to modify it, you can set it to -1. You need superuser privileges to perform permission modification operations.

Only supported on Unix.

Syntax

chown()The method syntax is as follows:

os.chown(path, uid, gid);

Parameters

  • path-- File path for setting permissions

  • uid-- Owner user ID

  • gid-- Owner group ID

Return Value

This method has no return value.

Examples

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

#!/usr/bin/python3

import os, sys

# 假定 /tmp/foo.txt 文件存在.
# 设置所有者 ID 为 100 
os.chown("/tmp/foo.txt", 100, -1)

print ("修改权限成功!!")

The output of executing the above program is:

Permission modification successful!!

Python3 OS File/Directory Methods

Other Extensions