On this page
pwd
Prev: pty | Up: stdlib_module | Next: py_compile
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — pwd
Tags: #public #python3 #stdlib
Import
import pwd
Overview
Unix-only interface to the POSIX user account database via getpwuid(), getpwnam(), and getpwall(). Use pwd on macOS/Linux to resolve numeric UIDs from os.stat().st_uid into usernames, home directories, or login shells.
Quick Start
import os
import sys
if sys.platform != "win32":
import pwd
entry = pwd.getpwuid(os.getuid())
assert entry.pw_uid == os.getuid()
assert isinstance(entry.pw_name, str) and isinstance(entry.pw_dir, str)
Implementation Walkthrough
1. Resolving a File Owner's Username from os.stat()
Pair pwd.getpwuid() with stat / pathlib to display human-readable file ownership:
import os
import pwd
def file_owner(path: str) -> str:
uid = os.stat(path).st_uid
try:
return pwd.getpwuid(uid).pw_name
except KeyError:
return str(uid)
Gotchas & Boundaries
- Unix-only: Raises
ModuleNotFoundErroron Windows; usegetpass.getuser()orPath.home()when you only need the current user cross-platform. - Unmapped UIDs raise
KeyError: Containers or network mounts often have numeric UIDs not present in the local user database—always catchKeyErrorand fall back tostr(uid). pw_passwdis a placeholder ('x'or'*'): Modern Unix systems keep actual authentication hashes in restricted shadow stores, not inpwd.
Common Functions
- pwd.getpwall() —
getpwall()— Return a list of all available password database entries, in arbitrary order. - pwd.getpwnam() —
getpwnam(name, /)— Return the password database entry for the given user name. - pwd.getpwuid() —
getpwuid(uidobj, /)— Return the password database entry for the given numeric user ID.
Classes & Objects
- pwd.struct_passwd — pwd.struct_passwd: Results from getpw*() routines. (Methods:
.count(),.index())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/grp
- tuts/python3/stdlib_module/pty
- tuts/python3/stdlib_module/pwd/getpwall
- tuts/python3/stdlib_module/pwd/getpwnam
- tuts/python3/stdlib_module/pwd/getpwuid
- tuts/python3/stdlib_module/pwd/struct_passwd
- tuts/python3/stdlib_module/py_compile
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/pty Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pwd/getpwall Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pwd/getpwnam Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pwd/getpwuid Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pwd/struct_passwd Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/py_compile 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