Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.lchown() Method

Python3 os.lchown() Method

Python3 OS File/Directory Methods Python3 OS File/Directory Methods


Overview

The os.lchown() method changes the file owner, similar to chown, but without following links.

Only supported on Unix.

Syntax

The **lchown()** method syntax is as follows:


os.lchown(path, uid, gid)

Parameters

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

# Open file
path = "/var/www/html/foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )

# Close open files
os.close( fd )

# Change file permissions
# Set file owner user ID
os.lchown( path, 500, -1)

# Set file owner group ID
os.lchown( path, -1, 500)

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

The output of the above program is:


修改权限成功!!

Python3 OS File/Directory Methods Python3 OS File/Directory Methods