How to change the permission of a file using Python?

ProgrammingPythonServer Side Programming

The user whose actions are allowed on a file are governed by its file permissions. A file's permissions for reading, writing, and executing are modified when the file's permissions are changed.

This article will cover how to change a file's permission in Python.

Using os.chmod() Method

To modify the permissions of a file, use the os.chmod () method.

Syntax

Following is the syntax for os.chmod() method −

os.chmod(path, mode)

Where, path represents the path of the file and mode contains different values as explained below.

There is no return value obtained in this method.

Os.chmod() modes

Following are the different mode arguments of os.chmod() −

  • stat.S_ISUID − On execution, set the group ID.
  • stat.S_ENFMT − Records must be locked.
  • stat.S_ISVTX − after execution, saves the text image
  • stat.S_IREAD − Reading by owner.
  • stat.S_IWRITE − Writing by owner.
  • stat.S_IEXEC − Execution by owner.
  • stat.S_IRWXU − Reading, writing, and execution by owner
  • stat.S_IRUSR − Reading by owner
  • stat.S_IWUSR − Writing by owner.
  • stat.S_IXUSR − Execution by owner.
  • stat.S_IRWXG − Reading, writing, and execution by group
  • stat.S_IRGRP − Reading by group
  • stat.S_IWGRP − Writing by group
  • stat.S_IXGRP − Execution by group
  • stat.S_IRWXO − Reading, writing, and execution by others.
  • stat.S_IROTH − Reading by others
  • stat.S_IWOTH − Writing by others
  • stat.S_IXOTH − Execution by others

Example - 1

Following is an example to change the permission of a file −

import os import sys import stat # Setting the given file to be read by the owner. os.chmod("C:\Users\Lenovo\Downloads\Work TP\trial.py", stat.S_IREAD) print("The file can only be ready by owner") # Setting the given file to be read by group. os.chmod("C:\Users\Lenovo\Downloads\Work TP\trial.py", stat.S_IRGRP ) print("The file access gets changed, now it can be read by group.")

Output

Following is an output of the above code −

The file can only be ready by owner
The file access gets changed, now it can be read by group

Example - 2

Following is an example to change the permission of a file −

import os import sys import stat # Setting the given file to be read, write and execute by group. os.chmod("C:\Users\Lenovo\Downloads\Work TP\trial.py", stat.S_IRWXG ) print("The file can be read, write and execute by group" # Setting the given file to be read, write and execute by others. os.chmod("C:\Users\Lenovo\Downloads\Work TP\trial.py", stat.S_IRWXO ) print("The file access gets changed, now it can be read, write and execute by others.")

Output

Following is an output of the above code −

The file can be read, write and execute by group
The file access gets changed, now it can be read, write and execute by others

Using Linux

We can change the permission of a file or a directory in Linux using subprocess.call() function. Python's subprocess contains a call() method that is utilised to start an application.

Example

Following is an example to change the permission of a file using subprocess.call() function −

import subprocess subprocess.call(['chmod', '0444', 'myFile.txt'])

Output

As an output we will be able to see that the permission of the file got changed.

raja
Updated on 18-Aug-2022 07:28:49

Advertisements