Python os.fchmod() method
Python OS file/directory methods
Overview
The os.fchmod() method changes the access permissions of a file specified by the parameter fd; the parameter mode is the Unix file access permission.
Available on Unix.
Syntax
The **fchmod()** method syntax is as follows:
os.fchmod(fd, mode);
Parameters
-
fd -- The file descriptor
-
mode -- Can consist of one or more of the following, separated by "|" when there are several:
-
stat.S_ISUID:Set the UID bit
-
stat.S_ISGID: Set the 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
-
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
stat.S_ISUID:Set the UID bit
-stat.S_ISGID: Set the 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
-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
# Open file "/tmp/foo.txt"
fd = os.open( "/tmp", os.O_RDONLY )
# Set file executable by group
os.fchmod( fd, stat.S_IXGRP)
# Set file writable by other users
os.fchmod(fd, stat.S_IWOTH)
print "修改权限成功!!"
# Close file
os.close( fd )
The output of the above program is:
修改权限成功!!