Python os.lchown() Method
Python OS File/Directory Methods
Overview
The os.lchown() method is used to change the file owner, similar to chown, but does not follow links.
Only supported on Unix.
Syntax
lchown()The method syntax is as follows:
os.lchown(path, uid, gid)
Parameters
pathThe file path for setting permissions
uidOwner user ID
gidOwner 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 # 打开文件 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!!Other Extensions