Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the os.Ichmod() method in Python?

Salman Yousaf

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

This os.lchmod() method is used to change the mode of the path to a numeric value. If the path is a system linksymlink, it affects the system link rather than the target. This method is equivalent to the os.chmod() method in the older Python version.

Note: The new python 3.6 version also accepts path-like objects as an argument value.

Syntax


os.lchmod(path, mode)

Parameters

It takes the following argument values:

  • path: The path of the file.
  • mode: Takes the below argument values or their combination with logical operators:
Different mode values

mode

Description

stat.S_ISUID

Set the user ID on execution.

stat.S_ISGID

Set the group ID on execution.

stat.S_ENFMT

Record locking enforced.

stat.S_ISVTX

Save text image after the execution.

stat.S_IREAD

Read by the owner.

stat.S_IWRITE

Write by the owner.

stat.S_IEXEC

Execute by the root owner.

stat.S_IRWXU

Read, write, and execute by root owner.

stat.S_IRUSR

Read by root owner.

stat.S_IWUSR

Write by root owner.

stat.S_IXUSR

Excecute by root owner.

stat.S_IRWXG

Read, write, and execute by the group.

stat.S_IRGRP

Read by the group.

stat.S_IWGRP

Write by the group.

stat.S_IXGRP

Execute by the 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 value

It doesn’t return any value.

Code

Let’s look at the code below:

main.py
demo.txt
# Demo example
import os,stat,sys
# open a file
path = "demo.txt"
# opening file
fd = os.open(path, os.O_RDWR|os.O_CREAT)
# Set a file execute by group.
os.chmod(path, stat.S_IREAD)
# Close opened file
os.close(fd)
print("File mode changed successfully!")
lchmod() is equivalent to chmod()

Explanation

Let’s discuss the lchmod() method from OSOperating System module in detail:

  • Line 4: We create a path string with the file named demo.txt.
  • Line 6: We invoke open() method in read-write mode.
  • Line 8: We call os.chmod() method with first argument path as path in stat.S_IXGRP mode. As os.lchmod() is equivalent to os.chmod() method in UNIX systems.
  • Line 10: We close the opened file.
  • Line 11:We print the message to the console.
Expected Output

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring