Python3 os.lchmod() Method

Python3 OS File/Directory Methods


Overview

The os.lchmod() method is used to modify the permissions of a link file.

It is only supported on Unix.

Syntax

lchmod()The method syntax is as follows:

os.lchmod(path, mode)

Parameters

  • path-- The file path for which the mode is to be set

  • mode-- Can be one or more of the following, multiple separated by "|":

    • stat.S_ISUID:Set UID bit

    • stat.S_ISGID:Set group ID bit

    • stat.S_ENFMT:System file lock enforcement action

    • stat.S_ISVTX:Save text and images after execution

    • stat.S_IREAD:Read permission for owner

    • stat.S_IWRITE:Write permission for owner

    • stat.S_IEXEC:Execute permission for owner

    • stat.S_IRWXU:Read, write, execute permission for owner

    • stat.S_IRUSR:Read permission for owner

    • stat.S_IWUSR:Write permission for owner

    • stat.S_IXUSR:Execute permission for owner

    • stat.S_IRWXG:Read, write, execute permission for group

    • stat.S_IRGRP:Read permission for group

    • stat.S_IWGRP:Write permission for group

    • stat.S_IXGRP:Execute permission for group

    • stat.S_IRWXO:Read, write, execute permission for others

    • stat.S_IROTH:Read permission for others

    • stat.S_IWOTH:Write permission for others

    • stat.S_IXOTH:Execute permission for others

Return Value

This method has no return value.

Example

The following example demonstrates the use of the lchmod() 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 )

# 修改文件权限
# 设置文件可以通过组执行
os.lchmod( path, stat.S_IXGRP)

# 设置文件可以被其他用户写入
os.lchmod("/tmp/foo.txt", stat.S_IWOTH)

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

The output of the above program is:

Permission modification successful!!

Python3 OS File/Directory Methods

Other Extensions