On this page
posixpath
Prev: posix | Up: stdlib_module | Next: pprint
Rel: pathlib, os, io, time, fileinput
Ref: docs.python.org — posixpath
Tags: #public #python3 #stdlib
Import
import posixpath
Overview
POSIX (slash-separated /a/b/c) path-manipulation implementation backing os.path on macOS, Linux, and BSD. For normal local filesystem work, use pathlib (Path) or os.path; import posixpath directly only when you explicitly need POSIX path semantics on any OS (such as normalizing URL paths, S3 keys, or container paths from Windows).
Quick Start
import posixpath
# Works identically on Windows, macOS, and Linux
joined = posixpath.join("/var/www", "assets", "logo.png")
assert joined == "/var/www/assets/logo.png"
assert posixpath.normpath("/a/b/../c/") == "/a/c"
Implementation Walkthrough
1. Normalizing Virtual or Remote POSIX Paths Cross-Platform
When manipulating paths inside zip archives, tarballs, Docker images, or object storage keys, posixpath (or pathlib.PurePosixPath) guarantees forward slashes regardless of host OS:
import posixpath
key = posixpath.join("releases", "2026", "v1.0.tar.gz")
directory, filename = posixpath.split(key)
assert directory == "releases/2026" and filename == "v1.0.tar.gz"
Gotchas & Boundaries
- Prefer
pathlib.PurePosixPathfor modern code:PurePosixPathgives the same OS-independent POSIX semantics with a cleaner object API. - Filesystem queries still hit the host OS: Pure string functions (
join,split,normpath) are OS-independent, butposixpath.exists()orexpanduser()query the local machine.
Common Functions
- posixpath.abspath() —
abspath(path)— Return an absolute path. - posixpath.basename() —
basename(p)— Returns the final component of a pathname - posixpath.commonpath() —
commonpath(paths)— Given a sequence of path names, returns the longest common sub-path. - posixpath.commonprefix() —
commonprefix(m)— Given a list of pathnames, returns the longest common leading component - posixpath.dirname() —
dirname(p)— Returns the directory component of a pathname - posixpath.exists() —
exists(path)— Test whether a path exists. Returns False for broken symbolic links - posixpath.expanduser() —
expanduser(path)— Expand ~ and ~user constructions. If user or $HOME is unknown, do nothing. - posixpath.expandvars() —
expandvars(path)— Expand shell variables of form $var and ${var}. Unknown variables are left unchanged. - posixpath.getatime() —
getatime(filename)— Return the last access time of a file, reported by os.stat(). - posixpath.getctime() —
getctime(filename)— Return the metadata change time of a file, reported by os.stat(). - posixpath.getmtime() —
getmtime(filename)— Return the last modification time of a file, reported by os.stat(). - posixpath.getsize() —
getsize(filename)— Return the size of a file, reported by os.stat(). - posixpath.isabs() —
isabs(s)— Test whether a path is absolute - posixpath.isdevdrive() —
isdevdrive(path)— Determines whether the specified path is on a Windows Dev Drive. - posixpath.isdir() —
isdir(s)— Return true if the pathname refers to an existing directory. - posixpath.isfile() —
isfile(path)— Test whether a path is a regular file - posixpath.isjunction() —
isjunction(path)— Test whether a path is a junction Junctions are not supported on the current platform - posixpath.islink() —
islink(path)— Test whether a path is a symbolic link - posixpath.ismount() —
ismount(path)— Test whether a path is a mount point - posixpath.join() —
join(a, *p)— Join two or more pathname components, inserting '/' as needed. - posixpath.lexists() —
lexists(path)— Test whether a path exists. Returns True for broken symbolic links - posixpath.normcase() —
normcase(s)— Normalize case of pathname. Has no effect under Posix - posixpath.normpath() —
normpath(path)— Normalize path, eliminating double slashes, etc. - posixpath.realpath() —
realpath(filename, *, strict=False)— Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path. - posixpath.relpath() —
relpath(path, start=None)— Return a relative version of a path - posixpath.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. - posixpath.sameopenfile() —
sameopenfile(fp1, fp2)— Test whether two open file objects reference the same file - posixpath.samestat() —
samestat(s1, s2)— Test whether two stat buffers reference the same file - posixpath.split() —
split(p)— Split a pathname. Returns tuple "(head, tail)" where "tail" is everything after the final slash. Either part may be empty. - posixpath.splitdrive() —
splitdrive(p)— Split a pathname into drive and path. On Posix, drive is always empty. - posixpath.splitext() —
splitext(p)— Split the extension from a pathname. - posixpath.splitroot() —
splitroot(p)— Split a pathname into drive, root and tail.
Constants & Attributes
posixpath.ALLOW_MISSING(ALLOW_MISSING) =os.path.ALLOW_MISSINGposixpath.altsep(NoneType) =Noneposixpath.curdir(str) ='.'posixpath.defpath(str) ='/bin:/usr/bin'posixpath.devnull(str) ='/dev/null'posixpath.extsep(str) ='.'posixpath.pardir(str) ='..'posixpath.pathsep(str) =':'posixpath.sep(str) ='/'posixpath.supports_unicode_filenames(bool) =True
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/genericpath
- tuts/python3/stdlib_module/posix
- tuts/python3/stdlib_module/posixpath/abspath
- tuts/python3/stdlib_module/posixpath/basename
- tuts/python3/stdlib_module/posixpath/commonpath
- tuts/python3/stdlib_module/posixpath/commonprefix
- tuts/python3/stdlib_module/posixpath/dirname
- tuts/python3/stdlib_module/posixpath/exists
- tuts/python3/stdlib_module/posixpath/expanduser
- tuts/python3/stdlib_module/posixpath/expandvars
- tuts/python3/stdlib_module/posixpath/getatime
- tuts/python3/stdlib_module/posixpath/getctime
- tuts/python3/stdlib_module/posixpath/getmtime
- tuts/python3/stdlib_module/posixpath/getsize
- tuts/python3/stdlib_module/posixpath/isabs
- tuts/python3/stdlib_module/posixpath/isdevdrive
- tuts/python3/stdlib_module/posixpath/isdir
- tuts/python3/stdlib_module/posixpath/isfile
- tuts/python3/stdlib_module/posixpath/isjunction
- tuts/python3/stdlib_module/posixpath/islink
- tuts/python3/stdlib_module/posixpath/ismount
- tuts/python3/stdlib_module/posixpath/join
- tuts/python3/stdlib_module/posixpath/lexists
- tuts/python3/stdlib_module/posixpath/normcase
- tuts/python3/stdlib_module/posixpath/normpath
- tuts/python3/stdlib_module/posixpath/realpath
- tuts/python3/stdlib_module/posixpath/relpath
- tuts/python3/stdlib_module/posixpath/samefile
- tuts/python3/stdlib_module/posixpath/sameopenfile
- tuts/python3/stdlib_module/posixpath/samestat
- tuts/python3/stdlib_module/posixpath/split
- tuts/python3/stdlib_module/posixpath/splitdrive
- tuts/python3/stdlib_module/posixpath/splitext
- tuts/python3/stdlib_module/posixpath/splitroot
- tuts/python3/stdlib_module/pprint
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/posix Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posixpath/abspath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posixpath/basename Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posixpath/commonpath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posixpath/commonprefix Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posixpath/dirname Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posixpath/exists Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posixpath/expanduser Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posixpath/expandvars Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posixpath/getatime Linked here; links to this page; shared tags: #stdlib