Python os.fchmod() Method

Python OS File/Directory Methods


Overview

The os.fchmod() method is used to change the access permissions of a file, which is specified by the parameter fd, and the parameter mode is the file access permission under Unix.

Available on Unix.

Syntax

fchmod()The method syntax is as follows:

os.fchmod(fd, mode);

Parameters

  • fd-- file descriptor

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

    • stat.S_ISUID:Set UID bit

    • stat.S_ISGID:Set group ID bit

    • stat.S_ENFMT:System V file locking enforcement

    • stat.S_ISVTX:Save text and images after execution

    • stat.S_IREAD:Read permission for the owner

    • stat.S_IWRITE:Write permission for the owner

    • stat.S_IEXEC:Execute permission for the owner

    • stat.S_IRWXU:Read, write, and execute permissions for the owner

    • stat.S_IRUSR:Read permission for the owner

    • stat.S_IWUSR:Write permission for the owner

    • stat.S_IXUSR:Execute permission for the owner

    • stat.S_IRWXG:Read, write, and execute permissions for the group

    • stat.S_IRGRP:Read permission for the group

    • stat.S_IWGRP:Write permission for the group

    • stat.S_IXGRP:Execute permission for the group

    • stat.S_IRWXO:Read, write, and execute permissions 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 fchmod() method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys, stat

# 打开文件 "/tmp/foo.txt"
fd = os.open( "/tmp", os.O_RDONLY )

# 设置文件可通过组执行

os.fchmod( fd, stat.S_IXGRP)

# 设置文件可被其他用户写入
os.fchmod(fd, stat.S_IWOTH)

print "修改权限成功!!"

# 关闭文件
os.close( fd )

The output of the above program is:

Permission modified successfully!!

Python OS File/Directory Methods

Other Extensions