Skip to content

Instantly share code, notes, and snippets.

@synackme
Last active August 29, 2015 14:00
Show Gist options
  • Save synackme/11280624 to your computer and use it in GitHub Desktop.
Save synackme/11280624 to your computer and use it in GitHub Desktop.
How to get the owner and grup of a folder with Python on Linux/Mac machines
#!/usr/bin/env python
# get the owner and group IDs of a directory using Python under Linux
"""
Use os.stat() to get the uid and gid of the file.
Then, use pwd.getpwuid() and grp.getgrgid() to
get the user and group names respectively.
"""
import grp
import pwd
import os
stat_info = os.stat('/path')
uid = stat_info.st_uid
gid = stat_info.st_gid
print uid, gid
user = pwd.getpwuid(uid)[0]
group = grp.getgrgid(gid)[0]
print user, group
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment