On this page
grp
Prev: graphlib | Up: stdlib_module | Next: gzip
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — grp
Tags: #public #python3 #stdlib
Import
import grp
Overview
Unix-only interface to the POSIX group database via getgrgid(), getgrnam(), and getgrall(). Use grp alongside pwd and os.stat().st_gid to resolve numeric group IDs into group names and member lists on macOS/Linux.
Quick Start
import os
import sys
if sys.platform != "win32":
import grp
entry = grp.getgrgid(os.getgid())
assert entry.gr_gid == os.getgid()
assert isinstance(entry.gr_name, str)
Implementation Walkthrough
1. Resolving File Group Ownership Safely
import os
import grp
def file_group(path: str) -> str:
gid = os.stat(path).st_gid
try:
return grp.getgrgid(gid).gr_name
except KeyError:
return str(gid)
Gotchas & Boundaries
- Unix-only: Unavailable on Windows (
ModuleNotFoundError). gr_memomits primary-group members: A user whose primary GID matches a group is typically not listed inentry.gr_mem—checkpwd.getpwuid().pw_gidas well if computing full group membership.- Catch
KeyError: Unmapped container or network GIDs raiseKeyError.
Common Functions
- grp.getgrall() —
getgrall()— Return a list of all available group entries, in arbitrary order. - grp.getgrgid() —
getgrgid(id)— Return the group database entry for the given numeric group ID. - grp.getgrnam() —
getgrnam(name)— Return the group database entry for the given group name.
Classes & Objects
- grp.struct_group — grp.struct_group: Results from getgr*() routines. (Methods:
.count(),.index())
Linked from
Related notes
- tuts/python3/stdlib_module/graphlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/grp/getgrall Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/grp/getgrgid Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/grp/getgrnam Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/grp/struct_group Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gzip Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/getopt Linked here; shared tags: #stdlib