Python3 os.lchown() Method

Python3 OS File/Directory Methods


Overview

The os.lchown() method is used to change the file owner, similar to chown, but it does not follow links.

Only supported on Unix.

Syntax

lchown()The method syntax is as follows:

os.lchown(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.

Example

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

#!/usr/bin/python3

import os, sys

# 打开文件
path = "/var/www/html/foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )

# 关闭打开的文件
os.close( fd )

# 修改文件权限
# 设置文件所属用户 ID
os.lchown( path, 500, -1)

# 设置文件所属用户组 ID
os.lchown( path, -1, 500)

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

The output of the above program is:

Permissions modified successfully!!

Python3 OS File/Directory Methods

Other Extensions