如何在python中找到文件或目录的所有者

如何在python中找到文件或目录的所有者

问题描述:

我需要Python中的函数或方法来查找文件或目录的所有者.

I need a function or method in Python to find the owner of a file or directory.

函数应类似于:

>>> find_owner("/home/somedir/somefile")
owner3

我并不是一个真正的python家伙,但是我能够将其鞭打起来:

I'm not really much of a python guy, but I was able to whip this up:

from os import stat
from pwd import getpwuid

def find_owner(filename):
    return getpwuid(stat(filename).st_uid).pw_name