为什么python的操作系统统计()告诉我我的文件组是空的?

2024-09-27 00:12:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在solaris5-10上使用python2.6.4。在

我有一个名为“myFile”的文件。它属于其他人,属于文件组(“mygrp”)中的我(“myuser”)。下面是我的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=[]) 

Tags: 文件infoospwdmyfilestatstgr
1条回答
网友
1楼 · 发布于 2024-09-27 00:12:29

根据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().)

这也许可以解释为什么你没有被列入结果,即使你是这个小组的一员。{a2和组的其他成员必须使用它。在

更新:根据评论中的link进一步解释:

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 

相关问题 更多 >

    热门问题