Last active
August 29, 2015 14:00
-
-
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
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
#!/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