如何在 Linux 机器上使用 Python 获取文件夹的所有者和组? [英] How to get the owner and group of a folder with Python on a Linux machine?

查看:28
本文介绍了如何在 Linux 机器上使用 Python 获取文件夹的所有者和组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux 下如何使用 Python 获取目录的所有者和组 ID?

How can I get the owner and group IDs of a directory using Python under Linux?

推荐答案

使用 os.stat() 获取文件的uid和gid.然后,使用 pwd.getpwuid()grp.getgrgid() 得到分别为用户名和组名.

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

这篇关于如何在 Linux 机器上使用 Python 获取文件夹的所有者和组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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