Skip to content

Instantly share code, notes, and snippets.

@kaiw
Created January 11, 2013 21:28
Show Gist options
  • Save kaiw/4514101 to your computer and use it in GitHub Desktop.
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.
# 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