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
How can I get a file\'s permission mask using Python?
To get a file's permission mask in Python, use the os.stat() method from the os module. This method performs a stat system call on the given path and returns file information including permissions.
Basic Usage of os.stat()
The os.stat() function returns a 10-member tuple containing file metadata ?
import os
# Create a sample file first
with open("sample.txt", "w") as f:
f.write("Hello World")
# Get file statistics
st = os.stat("sample.txt")
print("File stats tuple:", st)
File stats tuple: os.stat_result(st_mode=33188, st_ino=123456, st_dev=2049, st_nlink=1, st_uid=1000, st_gid=1000, st_size=11, st_atime=1640995200, st_mtime=1640995200, st_ctime=1640995200)
Extracting Permission Information
The first element st_mode contains the file permissions. You can access it using index [0] or the attribute name ?
import os
with open("sample.txt", "w") as f:
f.write("Hello World")
st = os.stat("sample.txt")
# Get permission mask using index
mode_by_index = st[0]
print("Mode by index:", mode_by_index)
# Get permission mask using attribute (preferred)
mode_by_attr = st.st_mode
print("Mode by attribute:", mode_by_attr)
# Convert to octal format for readable permissions
octal_permissions = oct(st.st_mode)[-3:]
print("Octal permissions:", octal_permissions)
Mode by index: 33188 Mode by attribute: 33188 Octal permissions: 644
Using stat Module for Better Readability
The stat module provides constants to interpret file permissions more easily ?
import os
import stat
with open("sample.txt", "w") as f:
f.write("Hello World")
st = os.stat("sample.txt")
# Extract just the permission bits
permissions = stat.S_IMODE(st.st_mode)
print("Permission mask:", oct(permissions))
# Check specific permissions
is_readable = bool(st.st_mode & stat.S_IRUSR)
is_writable = bool(st.st_mode & stat.S_IWUSR)
is_executable = bool(st.st_mode & stat.S_IXUSR)
print("Owner can read:", is_readable)
print("Owner can write:", is_writable)
print("Owner can execute:", is_executable)
Permission mask: 0o644 Owner can read: True Owner can write: True Owner can execute: False
Complete Permission Analysis
Here's a comprehensive example that displays all permission information ?
import os
import stat
def analyze_permissions(filepath):
st = os.stat(filepath)
mode = st.st_mode
# Get octal representation
octal_perms = oct(stat.S_IMODE(mode))
# Check file type
if stat.S_ISDIR(mode):
file_type = "Directory"
elif stat.S_ISREG(mode):
file_type = "Regular file"
else:
file_type = "Other"
print(f"File: {filepath}")
print(f"Type: {file_type}")
print(f"Permissions: {octal_perms}")
print(f"Raw mode: {mode}")
return mode
# Create and analyze a file
with open("test_file.txt", "w") as f:
f.write("Test content")
analyze_permissions("test_file.txt")
File: test_file.txt Type: Regular file Permissions: 0o644 Raw mode: 33188
Conclusion
Use os.stat() to get file permissions in Python. Access the mode with st.st_mode or st[0], and use the stat module for better permission interpretation and analysis.
