Python os.lchmod() Method

Python OS File/Directory Methods


Overview

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

Only supported on Unix.

Syntax

lchmod()The method syntax is as follows:

os.lchmod(path, mode)

Parameters

  • pathpath -- the file path for setting the mode

  • modemode -- can be one or more of the following, separated by "|":

    • stat.S_ISUID:Set UID bit

    • stat.S_ISGID:Set group ID bit

    • stat.S_ENFMT:Record locking enforcement

    • 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, and 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, and 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, and 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/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 )

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

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

print "修改权限成功!!"

The output of the above program is:

Permissions modified successfully!!

Python OS File/Directory Methods

Other Extensions