You are correct that os.access , as basic access , checks for a specific user (real and not effective identifiers to deal with suid situations).
os.stat is the right way to get more general information about a file, including permissions for each user, group, and others. The st_mode attribute of the object returned by os.stat has a permission bit for the file.
To interpret these bits, you can use the stat module. In particular, you want the bitmasks to be defined here , and you will use the & operator (bits) to use them to mask the corresponding bits in this st_mode attribute - for example, if you just need a True / False check, is there a specific file available for the group, one approach:
import os import stat def isgroupreadable(filepath): st = os.stat(filepath) return bool(st.st_mode & stat.S_IRGRP)
Take care: calling os.stat can be a little expensive, so be sure to retrieve all the information you care about with a single call, rather than repeating calls for each piece of interest; -).
Alex Martelli Dec 07 '09 at 18:31 2009-12-07 18:31
source share