Question:
How do I efficiently use the stat function to get meaningful file permissions (User, Group and Other).
Details:
I am querying the file permissions like so:
statInfo = os.stat
permissions = stat.S_IMODE ( os.stat ( 'fooBar.txt' ).st_mode )
This returns the permissions in decimal form. So if fooBar.txt
has the octal file permissions 0700
, here permissions
is set to the decimal value448
. What I want is to set 9 variables for each permission (ownerRead
, ownerWright
, ownerExecute
, groupRead
,...) If I was going to do this, I'd use a brute force method like so:
statInfo = os.stat
permissions = stat.S_IMODE ( os.stat ( 'fooBar.txt' ).st_mode )
octPermissions = oct ( permissions )
ownerRead = octPermissions [1] >= 4
ownerWrite = octPermissions [1] == 2 or octPermissions [1] == 6 or octPermissions [1] == 3 or
ownerExecute = octPermissions [1] == 1 or octPermissions [1] == 5 or octPermissions [1] == 3
Is there a more efficient way to do this without having to convert to octal as this function will get called quite a bit?
global
if you are going to alter them. Since I doubt you'll be modifying theos
andstat
modules, you do not need to mark them asglobal
anywhere. – Steatiteglobal
as they will be looked up from the module scope automatically without that. – Steatite