Using Python's stat function to efficiently get owner, group and other permissions
Asked Answered
A

3

6

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?

Audile answered 24/5, 2012 at 16:29 Comment(3)
You only need to mark names as global if you are going to alter them. Since I doubt you'll be modifying the os and stat modules, you do not need to mark them as global anywhere.Steatite
oops, my bad, I copied this from inside a functionAudile
I know, I guessed. Let me tell you again: You do not need to declare these global as they will be looked up from the module scope automatically without that.Steatite
H
6

You could use the bitwise AND operator:

m = os.stat('fooBar.txt').st_mode
otherExec  = bool(m & 0001)
otherWrite = bool(m & 0002)
otherRead  = bool(m & 0004)
groupExec  = bool(m & 0010)
groupWrite = bool(m & 0020)
groupRead  = bool(m & 0040)
...
Horseflesh answered 24/5, 2012 at 16:35 Comment(0)
S
5

Use bitwise AND and the constants in the stat module:

import stat
import os

mode = os.stat('fooBar.txt').st_mode

otherRead  = bool(mode & stat.S_IROTH)
otherWrite = bool(mode & stat.S_IWOTH)
otherExec  = bool(mode & stat.S_IXOTH)
...

So much more readable.

Steatite answered 24/5, 2012 at 16:43 Comment(1)
dang it, I just finished perfectly aligning my 0100's and 0010'sAudile
W
3

I've put this together into a function that gets UNIX permissions:

import os
from stat import (S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP,
                  S_IXGRP, S_IROTH, S_IWOTH, S_IXOTH)

def bit2int(bit):
    return int(oct(bit))

def convert_st_mode(st_mode):
    bits = (S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP,
            S_IROTH, S_IWOTH, S_IXOTH)
    mode = "%03d" % sum(int(bool(st_mode & bit)) * bit2int(bit) for bit in bits)
    return mode

def get_unix_permissions(pth):
    mode = convert_st_mode(os.stat(pth).st_mode)
    return mode

Usage:

mode = get_unix_permissions("somefile")
print(mode)
Wretched answered 24/12, 2013 at 1:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.