How to check the permissions of a file using Python?

PythonServer Side ProgrammingProgramming

You can use os.access(path, mode) to check the file permission with modes for reading, writing and execution permissions. For example,

>>> import os
>>> os.access('my_file', os.R_OK) # Check for read access
True
>>> os.access('my_file', os.W_OK) # Check for write access
True
>>> os.access('my_file', os.X_OK) # Check for execution access
False
>>> os.access('my_file', os.F_OK) # Check for existance of file
True

You can also use os.stat to get the status of a file or a file descriptor. It is quite complex to interpret as it uses bitmasks to identify the permissions. You can read mode about it here: https://docs.python.org/3/library/os.html#os.stat

raja
Published on 22-Dec-2017 12:11:17
Advertisements