Created
January 11, 2013 21:28
-
-
Save kaiw/4514101 to your computer and use it in GitHub Desktop.
Python snippet for formatting integral file permissions (e.g., as obtained from os.stat) in the familiar rwx layout. Released under CC0, if appropriate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First pass | |
mode = 500 | |
perms = [] | |
for group_index in range(2, -1, -1): | |
group = mode >> (group_index * 3) & 7 | |
perms.extend([p if group & 1 << 2 - i else '-' for i, p in enumerate(('r', 'w', 'x'))]) | |
print "".join(perms) | |
# Second pass | |
mode = 500 | |
perms = [] | |
rwx = ((4, 'r'), (2, 'w'), (1, 'x')) | |
for group_index in (6, 3, 0): | |
group = mode >> group_index & 7 | |
perms.extend([p if group & i else '-' for i, p in rwx]) | |
print "".join(perms) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment