On this page
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
- Internal implementation module: Not intended for direct import; use
os.pathor pathlib. - Character-based
commonprefix:os.path.commonprefix(["/a/cat", "/a/car"])returns"/a/ca", not"/a".
Common Functions
- genericpath.commonprefix() —
commonprefix(m)— Given a list of pathnames, returns the longest common leading component - genericpath.exists() —
exists(path)— Test whether a path exists. Returns False for broken symbolic links - genericpath.getatime() —
getatime(filename)— Return the last access time of a file, reported by os.stat(). - genericpath.getctime() —
getctime(filename)— Return the metadata change time of a file, reported by os.stat(). - genericpath.getmtime() —
getmtime(filename)— Return the last modification time of a file, reported by os.stat(). - genericpath.getsize() —
getsize(filename)— Return the size of a file, reported by os.stat(). - genericpath.isdevdrive() —
isdevdrive(path)— Determines whether the specified path is on a Windows Dev Drive. - genericpath.isdir() —
isdir(s)— Return true if the pathname refers to an existing directory. - genericpath.isfile() —
isfile(path)— Test whether a path is a regular file - genericpath.isjunction() —
isjunction(path)— Test whether a path is a junction Junctions are not supported on the current platform - genericpath.islink() —
islink(path)— Test whether a path is a symbolic link - genericpath.lexists() —
lexists(path)— Test whether a path exists. Returns True for broken symbolic links - genericpath.samefile() —
samefile(f1, f2)— Test whether two pathnames reference the same actual file or directory This is determined by the device number and i-node number and raises an exception if an os.stat() call on either pathname fails. - genericpath.sameopenfile() —
sameopenfile(fp1, fp2)— Test whether two open file objects reference the same file - genericpath.samestat() —
samestat(s1, s2)— Test whether two stat buffers reference the same file
Constants & Attributes
genericpath.ALLOW_MISSING(ALLOW_MISSING) =os.path.ALLOW_MISSING
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/gc
- tuts/python3/stdlib_module/genericpath/commonprefix
- tuts/python3/stdlib_module/genericpath/exists
- tuts/python3/stdlib_module/genericpath/getatime
- tuts/python3/stdlib_module/genericpath/getctime
- tuts/python3/stdlib_module/genericpath/getmtime
- tuts/python3/stdlib_module/genericpath/getsize
- tuts/python3/stdlib_module/genericpath/isdevdrive
- tuts/python3/stdlib_module/genericpath/isdir
- tuts/python3/stdlib_module/genericpath/isfile
- tuts/python3/stdlib_module/genericpath/isjunction
- tuts/python3/stdlib_module/genericpath/islink
- tuts/python3/stdlib_module/genericpath/lexists
- tuts/python3/stdlib_module/genericpath/samefile
- tuts/python3/stdlib_module/genericpath/sameopenfile
- tuts/python3/stdlib_module/genericpath/samestat
- tuts/python3/stdlib_module/getopt
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/gc Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/genericpath/commonprefix Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/genericpath/exists Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/genericpath/getatime Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/genericpath/getctime Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/genericpath/getmtime Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/genericpath/getsize Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/genericpath/isdevdrive Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/genericpath/isdir Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/genericpath/isfile Linked here; links to this page; shared tags: #stdlib