Change language

Python | os.fchmod () method

On Unix-like systems, the modes — these are the file system permissions granted to users, groups, and other classes to access the file. 
os.fchmod() in Python is used to change the file mode specified by the specified file descriptor to the specified numeric mode. This method is equivalent to os.chmod (fd, mode) .

Note. This the 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, an d 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: using the os method .fchmod ()

# Python program to explain os.fchmod () method definitions

 
# import of the os module

import os

 
# import of statistics module

import stat

  
# Filename

filename = "file.txt"

 
# Open the specified file and
# get file descriptor
# related with this
# os.open () method

fd = os. open (filename, os.O_RDWR)

 

 
# Print the current numeric mode
# (octal) file

mode = oct (os.stat (fd) .st_mode) [ - 3 :]

print ( "Current numeric mode of the file (octal notation):" , mode)

 

 
# Now change the mode
File #

 
# octal 777 as mode value
# read, write and execute permission
# for owner, group and others

mode = 0o777

os.fchmod (fd, mode)

print ( "File mode changed successfully" )

 
# Print changed numeric mode
# (octal) file

mode = oct (os.stat (fd) .st_mode) [ - 3 :]

print ( "Current numeric mode of the file (octal notation):" , mode)

  

  
The # mode parameter can also be
# defined by flags defined in
# Stat module

 
# Chnage mode

mode = stat.S_IRWXU

os.fchmod (fd, mode)

print ( "File mode changed successfully" )

print ( "Now, File can be read, write and execute d by owner only " )

  
# Print modified numeric mode
# (octal) of 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 ( "File mode changed successfully" )

print ("Now, File can be read, write and executed

by owner but can be read by group ")

  
# Print modified numeric mode
# (octal) file

mode = oct (os.stat (fd) .st_mode) [ - 3 :]

p rint ( "Current numeric mode of the file (octal notation):" , mode)

 

 
# Close the file descriptor
os.close (fd)

Exit :

 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 

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically