how to find the owner of a file or directory in python

ramdaz picture ramdaz · Dec 2, 2009 · Viewed 50.5k times · Source

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

The function should be like:

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

Answer

asveikau picture asveikau · Dec 2, 2009

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