On this page
  1. grp
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Resolving File Group Ownership Safely
  7. Gotchas & Boundaries
  8. Common Functions
  9. Classes & Objects

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

Common Functions

Classes & Objects

Linked from

Related notes