views:

616

answers:

4

Title says it all pretty much. Is there any way how I can find out about the owner and group of a folder with Python?

Thank you so much, you guys are amazing!

A: 

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_ino (inode number), st_dev (device), 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 os.stat above.

Jarret Hardie
A: 

Use the os.stat function.

eduffy
+5  A: 

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
Ayman Hourieh
A: 

Use os.stat:

>>> 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 stat.

Stephan202