- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the permission of a file using Python?
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.
- Related Articles
- How to change the permission of a directory using Python?
- How to change the mode of a file using Python?
- How to change the owner of a file using Python?
- How to change file or directory permission in Linux/Unix?
- How can I get a file's permission mask using Python?
- How to change SMB shared folder access permission using PowerShell?
- How to change file permissions in Python?
- How to change file extension in Python?
- How to disable delete permission of File and Directory in Linux?
- How to check the permissions of a file using Python?
- How to change the owner of a directory using Python?
- How to create a duplicate file of an existing file using Python?
- How to get stat of a file using Python?
- How to create hardlink of a file using Python?
- How to create softlink of a file using Python?
