On UNIX-like operating systems, new files are created with a default set of permissions. We can restrict or grant any specific or set of permissions by applying a permission mask. Using Python, we can get or set the permission mask for a file.
In this article, we will discuss how to get the permission mask for a file in Python.
Method used -
os.stat () : This method is used to performs
stat ()system call on the specified path. This method is used to get status of the specified path.
Below is a Python program to get a mask of file permissions.
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
Below is a short version of the above program —
File permission mask: 644