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

发布于 2024-11-07 18:04:12 字数 144 浏览 0 评论 0原文

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薔薇婲 2024-11-14 18:04:13

您可以使用id -u wong2来获取用户的uid
您可以使用 python 执行此操作:

import os 
def getUidByUname(uname):
    return os.popen("id -u %s" % uname).read().strip()

然后使用 id 到 os.chown

You can use id -u wong2 to get the uid of a user
You can do this with python:

import os 
def getUidByUname(uname):
    return os.popen("id -u %s" % uname).read().strip()

Then use the id to os.chown

溺渁∝ 2024-11-14 18:04:12
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)
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)
救赎№ 2024-11-14 18:04:12

从Python 3.3开始
https://docs.python.org/3.3/library/shutil.html#shutil.chown< /a>

import shutil
shutil.chown(path, user=None, group=None)

更改给定路径的所有者用户和/或组。

user 可以是系统用户名或uid;
这同样适用于团体。

至少需要一个参数。

可用性:Unix。

Since Python 3.3
https://docs.python.org/3.3/library/shutil.html#shutil.chown

import shutil
shutil.chown(path, user=None, group=None)

Change owner user and/or group of the given path.

user can be a system user name or a uid;
the same applies to group.

At least one argument is required.

Availability: Unix.

墨小沫ゞ 2024-11-14 18:04:12

由于 Shutil 版本支持组可选,因此我将代码复制并粘贴到我的 Python2 项目中。

https://hg.python.org/cpython/file/tip /Lib/shutil.py#l1010

def chown(path, user=None, group=None):
    """Change owner user and group of the given path.

    user and group can be the uid/gid or the user/group names, and in that case,
    they are converted to their respective uid/gid.
    """

    if user is None and group is None:
        raise ValueError("user and/or group must be set")

    _user = user
    _group = group

    # -1 means don't change it
    if user is None:
        _user = -1
    # user can either be an int (the uid) or a string (the system username)
    elif isinstance(user, basestring):
        _user = _get_uid(user)
        if _user is None:
            raise LookupError("no such user: {!r}".format(user))

    if group is None:
        _group = -1
    elif not isinstance(group, int):
        _group = _get_gid(group)
        if _group is None:
            raise LookupError("no such group: {!r}".format(group))

    os.chown(path, _user, _group)

Since the shutil version supports group being optional, I copy and pasted the code to my Python2 project.

https://hg.python.org/cpython/file/tip/Lib/shutil.py#l1010

def chown(path, user=None, group=None):
    """Change owner user and group of the given path.

    user and group can be the uid/gid or the user/group names, and in that case,
    they are converted to their respective uid/gid.
    """

    if user is None and group is None:
        raise ValueError("user and/or group must be set")

    _user = user
    _group = group

    # -1 means don't change it
    if user is None:
        _user = -1
    # user can either be an int (the uid) or a string (the system username)
    elif isinstance(user, basestring):
        _user = _get_uid(user)
        if _user is None:
            raise LookupError("no such user: {!r}".format(user))

    if group is None:
        _group = -1
    elif not isinstance(group, int):
        _group = _get_gid(group)
        if _group is None:
            raise LookupError("no such group: {!r}".format(group))

    os.chown(path, _user, _group)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文