Python os.lchown() method
Python 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
-
path -- The path of the file whose permissions are set
-
uid -- The owner user ID
-
gid -- The owner group ID
Return Value
This method has no return value.
Example
The following example demonstrates the use of the lchown() method:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
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:
修改权限成功!!