如何在python中找到文件或目录的所有者 [英] how to find the owner of a file or directory in python

查看:442
本文介绍了如何在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

这篇关于如何在python中找到文件或目录的所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆