如何按名称更改目录的用户和组权限?

2022-01-11 00:00:00 python operating-system chown

问题描述

os.chown 正是我想要的,但我想要按名称指定用户和组,而不是 ID(我不知道它们是什么).我该怎么做?

os.chown is exactly what I want, but I want to specify the user and group by name, not ID (I don't know what they are). How can I do that?


解决方案

import pwd
import grp
import os

uid = pwd.getpwnam("nobody").pw_uid
gid = grp.getgrnam("nogroup").gr_gid
path = '/tmp/f.txt'
os.chown(path, uid, gid)

相关文章