Python3 os.fchmod() Method

Python3 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, multiple separated by "|":

    • stat.S_ISUID:Set UID bit

    • stat.S_ISGID:Set group ID bit

    • stat.S_ENFMT:Enforcement action for system file locking

    • stat.S_ISVTX:Save text and images after execution

    • stat.S_IREAD:Read permission for the owner; synonymous with stat.S_IRUSR in Unix V7

    • stat.S_IWRITE:Write permission for the owner; synonymous with stat.S_IWUSR in Unix V7

    • stat.S_IEXEC:Execute permission for the owner; synonymous with stat.S_IXUSR in Unix V7

    • 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 same group

    • stat.S_IRGRP:Read permission for the same group

    • stat.S_IWGRP:Write permission for the same group

    • stat.S_IXGRP:Execute permission for the same group

    • stat.S_IRWXO:Read, write, and execute permissions for other groups

    • stat.S_IROTH:Read permission for other groups

    • stat.S_IWOTH:Write permission for other groups

    • stat.S_IXOTH:Execute permission for other groups

Return Value

This method has no return value.

Example

The following example demonstrates the use of the fchmod() method:

#!/usr/bin/python3

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!!

Python3 OS File/Directory Methods

Other Extensions