Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to change the permission of a file using Python?
File permissions determine which users can read, write, or execute a file. Python provides several methods to modify file permissions programmatically using the os.chmod() method and subprocess calls.
Using os.chmod() Method
The os.chmod() method changes file permissions by accepting a file path and permission mode ?
Syntax
os.chmod(path, mode)
Parameters:
- path ? The file or directory path
- mode ? Permission constants from the stat module
Return Value: None
Common Permission Constants
The stat module provides these permission constants ?
| Constant | Description | Octal Value |
|---|---|---|
stat.S_IRUSR |
Read by owner | 0400 |
stat.S_IWUSR |
Write by owner | 0200 |
stat.S_IXUSR |
Execute by owner | 0100 |
stat.S_IRGRP |
Read by group | 0040 |
stat.S_IWGRP |
Write by group | 0020 |
stat.S_IXGRP |
Execute by group | 0010 |
stat.S_IROTH |
Read by others | 0004 |
stat.S_IWOTH |
Write by others | 0002 |
stat.S_IXOTH |
Execute by others | 0001 |
Example 1: Setting Basic Permissions
import os
import stat
# Create a test file
with open("test_file.txt", "w") as f:
f.write("Hello World")
# Make file read-only for owner
os.chmod("test_file.txt", stat.S_IRUSR)
print("File set to read-only for owner")
# Make file readable and writable for owner
os.chmod("test_file.txt", stat.S_IRUSR | stat.S_IWUSR)
print("File set to read-write for owner")
File set to read-only for owner File set to read-write for owner
Example 2: Using Octal Notation
import os
# Create a test file
with open("demo_file.txt", "w") as f:
f.write("Demo content")
# Set permissions using octal notation
# 0o755 = rwxr-xr-x (owner: read,write,execute; group,others: read,execute)
os.chmod("demo_file.txt", 0o755)
print("Permissions set to 755 (rwxr-xr-x)")
# Set to read-only for all
os.chmod("demo_file.txt", 0o444)
print("Permissions set to 444 (r--r--r--)")
Permissions set to 755 (rwxr-xr-x) Permissions set to 444 (r--r--r--)
Using subprocess.call() on Linux
On Unix-like systems, you can use the chmod command through subprocess ?
import subprocess # Make file executable for owner only subprocess.call(['chmod', '0700', 'script.sh']) # Make file readable by all, writable by owner only subprocess.call(['chmod', '0644', 'document.txt'])
Checking Current Permissions
import os
import stat
# Create a test file
with open("check_perms.txt", "w") as f:
f.write("Test file")
# Get current permissions
file_stat = os.stat("check_perms.txt")
permissions = oct(file_stat.st_mode)[-3:]
print(f"Current permissions: {permissions}")
# Change permissions
os.chmod("check_perms.txt", 0o644)
file_stat = os.stat("check_perms.txt")
permissions = oct(file_stat.st_mode)[-3:]
print(f"New permissions: {permissions}")
Current permissions: 666 New permissions: 644
Conclusion
Use os.chmod() with stat constants or octal notation to change file permissions in Python. The method works across platforms, while subprocess.call() provides Unix-specific chmod functionality.
Advertisements
