Change language

Python | os.chmod method

Syntax :

 os.chmod (path, mode) 

Parameters:
path - path name of the file or directory path
mode - mode may take one of the following values:

  • 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

Code # 1:

# Python program to explain the method os.chmod ()

 
# import required libraries

import os, sys, stat

 
# Set this file as read by the owner.

os.chmod ( "/ Geeks / gfg.txt" , stat.S_IREAD)

print ( "File can be read only by owner . " )

  
# Install this file read by others.

os.chmod ( "/ Geeks / gfg. txt " , stat.S_IROTH)

print ( "File access changed, can be read by others now." )

Exit:

 File can be read only by owner. File access changed, can be read by others now. 

Code # 2:

# Python program to explain the os.chmod () method

 
# import required libraries

import os, sys, stat

 
# Install the given file, written by the owner.

os.chmod ( "/ Geeks / gfg.txt" , stat.S_IWRITE)

 
# Install the given file, made by the owner.

os.chmod ( "/ Geeks / gfg.txt" , stat.S_IXUSR)

print ( "File can be written and executed only by owner." )

Exit:

 File can be written and executed only by owner. 

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