Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Files, Paths and Text I/O

The os.path module

Reference for Python's os.path functions: join, split, splitext, basename, dirname, exists, isfile, abspath, normpath, relpath and expanduser.

os.path is a collection of functions that work on paths stored as strings. Some are pure string operations (join, split, splitext, basename), and some ask the file system (exists, isfile, getsize). The functions use the rules of the operating system the program runs on.

New code often prefers pathlib, but os.path is everywhere in existing code, and its functions are short and simple. Every function here accepts Path objects too.

Python
import os.path

print(os.path.join("data", "2025", "report.csv"))

Try it

Python Pure string functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Asking the file system

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Most useful functions

Function Description
join(a, *parts) Joins parts with the right separator. An absolute part discards everything before it.
split(p) Returns (head, tail): folder and last component.
splitext(p) Returns (root, ext); the extension is the last suffix, with its dot. Leading dots (as in .bashrc) do not count.
basename(p), dirname(p) The last component, and everything before it. A trailing slash gives an empty basename.
exists(p), isfile(p), isdir(p), islink(p) Checks; they return False for missing paths and never raise for that.
getsize(p), getmtime(p) Size in bytes, modification time (seconds since the epoch). Raise OSError if the file is missing.
abspath(p), realpath(p) Absolute path, with .. collapsed; realpath also resolves symlinks.
normpath(p) Collapses duplicate separators and ./.. without touching the disk.
relpath(p, start=".") Path of p relative to start.
commonpath(paths), commonprefix(strings) Longest shared folder path; commonprefix is plain character-by-character matching.
expanduser(p), expandvars(p) Replace ~ with the home folder; replace $VAR with environment values.
isabs(p), samefile(a, b) Is the path absolute; do two paths refer to the same file.

Return value: strings, booleans or numbers as listed. Raises: OSError (such as FileNotFoundError) for getsize, getmtime and samefile on missing paths; TypeError if you mix str and bytes.

Usage notes

  • os.path follows the current operating system. On Windows it is ntpath (backslashes, drive letters); on Linux and macOS it is posixpath. The first example imports posixpath directly so the output is the same everywhere.
  • join() is not string concatenation. join("a", "/b") is /b. Never join user input that may start with / and expect it to stay inside a folder.
  • exists() is not a lock. The file can appear or vanish right after the check. Try the operation and handle the exception when it matters.
  • splitext() only splits the last suffix: "archive.tar.gz" gives ("archive.tar", ".gz").
  • normpath() can change meaning with symlinks, because a/symlink/.. is not always a. Use realpath() when links matter.
  • Case and separators. Windows paths compare case-insensitively; use os.path.normcase() before comparing strings, or use samefile().
  • expanduser("~") returns the string unchanged if no home folder can be found.
  • Prefer pathlib (Path.suffix, Path.parent, /) in new code; the functions above map one-to-one to its attributes.