Open In App

Python | os.fchmod() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
In Unix-like systems, Modes are file system permissions given to user, group and others classes to access a file. 
os.fchmod() method in Python is used to change the mode of a file given by the specified file descriptor to the specified numeric mode. This method is equivalent to os.chmod(fd, mode)
Note: This method is only available on Unix platforms. 
 

Syntax: os.fchmod(fd, mode)
Parameters: 
fd: A file descriptor whose mode is to be set. 
mode: A numeric value representing mode to be set. 
mode may also take one of the following values or bitwise ORed combinations of them: 
 



  • stat.S_ISUID : Set user ID on execution
  • stat.S_ISGID : Set group ID on execution
  • stat.S_ENFMT : Record locking enforced
  • stat.S_ISVTX : Save text image after execution
  • stat.S_IREAD : Read by owner.
  • stat.S_IWRITE : Write by owner.
  • stat.S_IEXEC : Execute by owner.
  • stat.S_IRWXU : Read, write, and execute by owner
  • stat.S_IRUSR : Read by owner
  • stat.S_IWUSR : Write by owner.
  • stat.S_IXUSR : Execute by owner.
  • stat.S_IRWXG : Read, write, and execute by group
  • stat.S_IRGRP : Read by group
  • stat.S_IWGRP : Write by group
  • stat.S_IXGRP : Execute by group
  • stat.S_IRWXO : Read, write, and execute by others.
  • stat.S_IROTH : Read by others
  • stat.S_IWOTH : Write by others
  • stat.S_IXOTH : Execute by others

Return type: This method does not return any value. 
 

Code: Use of os.fchmod() method 
 






# Python program to explain os.fchmod() method
 
# importing os module
import os
 
# importing stat module
import stat
 
# File name
filename = "file.txt"
 
# Open the specified file and
# get the file descriptor
# associated with it using
# os.open() method
fd = os.open(filename, os.O_RDWR)
 
 
# Print the current numeric mode
# (octal notation ) of the file
mode = oct(os.stat(fd).st_mode)[-3:]
print("Current numeric mode of the file (octal notation):", mode)
 
 
# Now change the mode
# of the file
 
# octal value 777 as mode means
# read write and execute permission
# for owner, group and others
mode = 0o777
os.fchmod(fd, mode)
print("\nFile mode changed successfully")
 
# Print the changed numeric mode
# (octal notation ) of the file
mode = oct(os.stat(fd).st_mode)[-3:]
print("Current numeric mode of the file (octal notation):", mode)
 
 
# mode parameter can be also
# given by flags defined in
# stat module
 
# Change mode
mode = stat.S_IRWXU
os.fchmod(fd, mode)
print("\nFile mode changed successfully")
print("Now, File can be read, write and executed by owner only")
 
# Print the changed numeric mode
# (octal notation ) of the file
mode = oct(os.stat(fd).st_mode)[-3:]
print("Current numeric mode of the file (octal notation):", mode)
 
 
 
# change mode
mode = stat.S_IRWXU | stat.S_IRGRP
os.fchmod(fd, mode)
print("\nFile mode changed successfully")
print("Now, File can be read, write and executed \
by owner but can be read by group")
 
# Print the changed numeric mode
# (octal notation ) of the file
mode = oct(os.stat(fd).st_mode)[-3:]
print("Current numeric mode of the file (octal notation):", mode)
 
 
# Close the file descriptor
os.close(fd)

Output: 
Current numeric mode of the file (octal notation): 666

File mode changed successfully
Current numeric mode of the file (octal notation): 777

File mode changed successfully
Now, File can be read, write and executed by owner only
Current numeric mode of the file (octal notation): 700

File mode changed successfully
Now, File can be read, write and executed by owner but can be read by group
Current numeric mode of the file (octal notation): 740

 


Article Tags :