Why does os.stat() tell me that my file-group has no members?

Hans Mulder hansmu at xs4all.nl
Wed Dec 19 18:17:16 EST 2012


On 19/12/12 22:40:00, saqib.ali.75 at gmail.com wrote:
> 
> 
> I'm using python 2.6.4 on Solaris 5-10.
> 
> I have a file named "myFile". It is owned by someone else, by
> I ("myuser") am in the file's group ("mygrp"). Below is my python
> code. Why does it tell me that mygrp has no members???
> 
> 
>>>> 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=[])

It doesn't say that your group has no members.

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.

Your experiment shows that nobody has "mygrp" as an additional
group.  So if you're a member of mygrp, then it must be your
primary group, i.e. os.getgid() should return 100 for you.

You can get a complete list of members of group by adding
two lists:

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


Hope this helps,

-- HansM



More information about the Python-list mailing list