On this page
  1. genericpath
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Avoid commonprefix() for Paths—Use os.path.commonpath()
  7. Gotchas & Boundaries
  8. Common Functions
  9. Constants & Attributes

genericpath

Prev: gc | Up: stdlib_module | Next: getopt
Rel: pathlib, os, io, time, fileinput
Ref: docs.python.org — genericpath
Tags: #public #python3 #stdlib


Import

import genericpath

Overview

Internal module holding platform-independent path helpers (commonprefix(), exists(), isfile(), isdir(), getsize(), getmtime(), samestat()) shared by posixpath and ntpath. Do not import genericpath directly—access these functions through os.path or pathlib.

Quick Start

import os
from pathlib import Path
import tempfile

with tempfile.TemporaryDirectory() as tmp:
    p = Path(tmp) / "demo.txt"
    p.write_text("hello", encoding="utf-8")
    # These os.path helpers are implemented in genericpath
    assert os.path.isfile(p) and os.path.getsize(p) == 5

Implementation Walkthrough

1. Avoid commonprefix() for Paths—Use os.path.commonpath()

genericpath.commonprefix() compares character-by-character rather than by path segment, which can produce invalid paths (e.g. "/usr/lib" and "/usr/local" yield "/usr/l"). Always use os.path.commonpath() instead.

Gotchas & Boundaries

Common Functions

Constants & Attributes

Linked from

Related notes