Each Answer to this Q is separated by one/two green lines.
How can I get the owner and group IDs of a directory using Python under Linux?
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) group = grp.getgrgid(gid) print user, group
Since Python 3.4.4, the
Path class of
pathlib module provides a nice syntax for this:
from pathlib import Path whatever = Path("relative/or/absolute/path/to_whatever") if whatever.exists(): print("Owner: %s" % whatever.owner()) print("Group: %s" % whatever.group())
>>> s = os.stat('.') >>> s.st_uid 1000 >>> s.st_gid 1000
st_uid is the user id of the owner,
st_gid is the group id. See the linked documentation for other information that can be acuired through
I tend to use os.stat:
Perform a stat system call on the given path. The return value is an object whose attributes correspond to the members of the stat structure, namely: st_mode (protection bits),
st_nlink(number of hard links),
st_uid(user id of owner),
st_gid(group id of owner),
st_size(size of file, in bytes),
st_atime(time of most recent access),
st_mtime(time of most recent content modification),
st_ctime(platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows)
There’s an example at the link to
If you are using Linux, it is much easier.
Install tree with the command yum install tree. Then execute the command ‘tree -a -u -g’