Prerequisite: Python | os.umask() method
In UNIX-like operating systems, new files are created with a default set of permissions. We can restrict or provide any specific or set of permissions by applying a permission mask. Using Python, we can get or set file’s permission mask.
In this article, we will discuss about how to get the permission mask of 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 the Python program to get a file’s permission mask –
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 program is the short version of above program –
File permission mask: 644
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.