gpt4 book ai didi

python - 为什么 python 的 os.stat() 告诉我我的文件组是空的?

转载 作者:行者123 更新时间:2023-11-28 17:48:47 25 4
gpt4 key购买 nike

我在 Solaris 5-10 上使用 python 2.6.4。

我有一个名为“myFile”的文件。它归其他人所有,我(“myuser”)在文件组(“mygrp”)中。下面是我的python代码。为什么它告诉我 mygrp 没有成员???

>>> import os, pwd, grp 
>>> stat_info = os.stat("myFile")
>>> fileUID = stat_info.st_uid
>>> fileGID = stat_info.st_gid
>>> fileGroup = grp.getgrgid(fileGID)[0]
>>> fileUser = pwd.getpwuid(fileUID)[0]
>>> print "grp.getgrgid(fileGID) = %s" % grp.getgrgid(fileGID)
grp.getgrgid(fileGID) = grp.struct_group(gr_name='mygrp', gr_passwd='', gr_gid=100, gr_mem=[])

最佳答案

根据 the docs (强调我的):

The gid is an integer, name and password are strings, and the member list is a list of strings. (Note that most users are not explicitly listed as members of the group they are in according to the password database. Check both databases to get complete membership information. Also note that a gr_name that starts with a + or - is likely to be a YP/NIS reference and may not be accessible via getgrnam() or getgrgid().)

这或许可以解释为什么您没有列在结果中,即使您是该组的成员。我相信你必须使用 pwd.getpwall并针对组 ID 对其进行过滤以获取该组的其余成员。

更新:进一步解释,根据 link OP 在评论中发布:

Every account has a primary group, and some accounts also have addtional groups. The primary group is the one in the .pw_gid attribute in the pwd entry. The additional groups are those that mention the account in the .gr_mem attribute in their grp entry.

检索群组所有成员的示例代码:

def all_members(gid):
primary_members = [user.pw_name for user in pwd.getpwall() if user.pw_gid == gid]
additional_members = grp.getgrgid(gid).gr_mem
return primary_members + additional_members

关于python - 为什么 python 的 os.stat() 告诉我我的文件组是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13961753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com