Open In App

How to get the permission mask of a file in Python

Last Updated : 23 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In UNIX-like operating systems, new files are created with a default set of permissions. We can restrict or provide any specific set of permissions by applying a permission mask. Using Python, we can get or set the file’s permission mask. In this article, we will discuss how to get the permission mask of a file in Python.

Get the Permission Mask of a File in Python

Below is an example by which we can perform changing file permissions in the Python OS Module as well as getting the permission mask of a file in Python. Before moving, firstly we understand the method that we have used in this example:

  • os.stat(): This method is used to perform stat() system calls on the specified path. This method is used to get the status of the specified path.

Example:

In this example, the os.stat() function is used to obtain the status of a file named file.txt. The script then accesses the file’s permission mask from the status. Finally, the permission mask is displayed in octal format using both direct extraction and bitwise operations.

Python3




# Import os module
import os
 
# File
filename = "./file.txt"
 
print("Status of %s:" % filename)
status = os.stat(filename)
 
print(status)
 
print("\nFile type and file permission mask:", status.st_mode)
 
print("File type and file permission mask(in octal):",
      oct(status.st_mode))
 
 
print("\nFile permission mask (in octal):", oct(status.st_mode)[-3:])
 
# Alternate way
print("File permission mask (in octal):", oct(status.st_mode & 0o777))


Output

Status of ./file.txt:
os.stat_result(st_mode=33188, st_ino=801303, st_dev=2056, st_nlink=1,
st_uid=1000, st_gid=1000, st_size=409, st_atime=1561590918, st_mtime=1561590910,
st_ctime=1561590910)

File type and file permission mask: 33188
File type and file permission mask(in octal): 0o100644

File permission mask (in octal): 644
File permission mask (in octal): 0o644


Shorter Version of the Previous Code

In this example, the script uses os.stat to fetch the status of file.txt. The last three digits of the file’s permission mask in octal format using oct() are then extracted and displayed.

Python3




import os
 
# File
filename = "./file.txt"
 
mask = oct(os.stat(filename).st_mode)[-3:]
 
# Print the mask
print("File permission mask:", mask)


Output

File permission mask: 644




Similar Reads

How to Fix: PermissionError: [Errno 13] Permission Denied in Python
When your Python code encounters a situation where it lacks the necessary permissions to access a file or directory, it raises PermissionError: [Errno 13] Permission denied in Python. This article will explore how to address the Errno 13 Error in Python effectively. What is PermissionError: [Errno 13] Permission Denied in Python?PermissionError: [E
3 min read
Adding Permission in API - Django REST Framework
There are many different scenarios to consider when it comes to access control. Allowing unauthorized access to risky operations or restricted areas results in a massive vulnerability. This highlights the importance of adding permissions in APIs. Django REST framework allows us to leverage permissions to define what can be accessed and what actions
7 min read
MoviePy – Checking Mask of Video File Clip
In this article we will see how we can check if the video file clip in MoviePy is a mask video or not. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. IsMask property tells that if the given video clip is mask or not, mask is basically that video which is used as mask for the another video.
2 min read
MoviePy – Adding Mask to Video File Clip
In this article we will see how we can add mask to the video file clip in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. IsMask property tells that if the given video clip is mask or not, mask is basically that video which is used as mask for the another video. We can make a video
2 min read
Python - Get file id of windows file
File ID is a unique file identifier used on windows to identify a unique file on a Volume. File Id works similar in spirit to a inode number found in *nix Distributions. Such that a fileId could be used to uniquely identify a file in a volume. We would be using an command found in Windows Command Processor cmd.exe to find the fileid of a file. In o
3 min read
Python Program to Get the File Name From the File Path
In this article, we will be looking at the program to get the file name from the given file path in the Python programming language. Sometimes during automation, we might need the file name extracted from the file path. Better to have knowledge of: Python OS-modulePython path moduleRegular expressionsBuilt in rsplit()Method 1: Python OS-moduleExamp
4 min read
How To Get The Uncompressed And Compressed File Size Of A File In Python
We are given a compressed file as well as uncompressed file. Our task is to get the size of uncompressed as well as compressed file in Python. In this article, we will see how we can get the uncompressed and compressed file size of a file in Python. What is Uncompressed And Compressed File?Uncompressed File: An uncompressed file is a file that has
3 min read
Python | Pandas dataframe.mask()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.mask() function return an object of same shape as self and whose corresponding entries are from self where cond is Fals
3 min read
Python | Pandas Series.mask()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.mask() function is used for masking purpose. This function replace values where
2 min read
Python program to mask a list using values from another list
Given two lists, the task is to write a python program that marks 1 for elements present in the other list else mark 0. Input : test_list = [5, 2, 1, 9, 8, 0, 4], search_list = [1, 10, 8, 3, 9]Output : [0, 0, 1, 1, 1, 0, 0]Explanation : 1, 9, 8 are present in test_list at position 2, 3, 4 and are masked by 1. Rest are masked by 0. Input : test_list
5 min read