platformdirs

platformdirs is a small, dependency-free Python library that tells your application where to put its files. Ask it for a data, config, cache, state, log or runtime directory and it returns the location that the current operating system expects: the XDG Base Directory layout on Linux, ~/Library on macOS, AppData on Windows, the app's private storage on Android, and the app container on iOS. It also knows the user's Documents, Downloads, Pictures, Videos, Music, Desktop and other personal folders.

This documentation covers platformdirs 4.14 (the latest release at the time of writing is 4.14.9). It explains every function and property, every parameter, every environment variable that changes a path, and the platform-specific behavior and safety checks that the library performs behind a one-line call.

The Problem It Solves

Every operating system has its own rules for where applications keep their files:

Purpose Linux macOS Windows
User data ~/.local/share/MyApp ~/Library/Application Support/MyApp C:\Users\<User>\AppData\Local\Acme\MyApp
User config ~/.config/MyApp ~/Library/Application Support/MyApp C:\Users\<User>\AppData\Local\Acme\MyApp
User cache ~/.cache/MyApp ~/Library/Caches/MyApp C:\Users\<User>\AppData\Local\Acme\MyApp\Cache
User logs ~/.local/state/MyApp/log ~/Library/Logs/MyApp C:\Users\<User>\AppData\Local\Acme\MyApp\Logs

Hard-coding any one of these makes an application wrong everywhere else, and handling them by hand means re-implementing environment variable overrides (XDG_CONFIG_HOME, XDG_CACHE_HOME, ...), the Windows Known Folder API, Homebrew prefixes, Android package folders and a long list of edge cases. platformdirs does all of that, so your code stays platform-agnostic:

from platformdirs import user_config_path

config_file = user_config_path("MyApp", "Acme") / "settings.toml"

Key Features

  • Automatic platform detection: Linux and other Unix systems (including FreeBSD, OpenBSD and NetBSD), macOS, Windows, Android and iOS, selected once at import time. See How platformdirs Works.
  • App directories: data, config, cache, state, log, runtime and preference directories, each with a per-user (user_*) and, where it makes sense, a system-wide (site_*) variant. See Choosing a Directory.
  • User media directories: documents, downloads, pictures, videos, music, desktop, projects, public share, templates and fonts, plus the bin and applications directories.
  • str or Path: every directory exists as a *_dir property or function returning str and a *_path variant returning pathlib.Path. See Module Functions.
  • Standard overrides: honors the XDG variables on Linux and macOS, the user-dirs.dirs file on Linux, and its own WIN_PD_OVERRIDE_* variables on Windows. See Environment Variables.
  • Optional directory creation: ensure_exists=True creates directories when you read them, with private 0700 permissions for user directories on POSIX systems. See Parameters.
  • File helpers: place_config_file() and friends create the parent directories of a file safely, and find_config_file() and friends search the user directory, then the site directories. See The PlatformDirs Class.
  • Search-path iterators: iter_config_paths() and friends yield the user directory followed by every site directory, ready for layered configuration.
  • Test isolation: platformdirs.testing.isolated_dirs() and the bundled platformdirs_isolated pytest fixture redirect every directory into a temporary folder. See Testing Helpers.
  • Security-minded: refuses directory names that escape their base, validates XDG_RUNTIME_DIR ownership and permissions, and checks ownership of shared parents before creating directories. See Directory Creation and Safety.
  • Drop-in replacement for appdirs: AppDirs is an alias of PlatformDirs, and every appdirs function exists. See Migrating from appdirs.

A Short Example

from platformdirs import PlatformDirs

dirs = PlatformDirs("SuperApp", "Acme", version="2.0", ensure_exists=True)

print(dirs.user_data_dir)    # where the database lives
print(dirs.user_config_dir)  # where settings live
print(dirs.user_cache_dir)   # safe to delete
print(dirs.user_log_dir)     # log files

settings = dirs.place_config_file("settings.toml")  # parent directories created, mode 0700
settings.write_text('theme = "dark"\n', encoding="utf-8")

On Linux, without XDG overrides, this prints:

/home/<user>/.local/share/SuperApp/2.0
/home/<user>/.config/SuperApp/2.0
/home/<user>/.cache/SuperApp/2.0
/home/<user>/.local/state/SuperApp/2.0/log

On Windows it prints C:\Users\<User>\AppData\Local\Acme\SuperApp\2.0, ...\Acme\SuperApp\2.0, ...\Acme\SuperApp\Cache\2.0 and ...\Acme\SuperApp\2.0\Logs. Note how Cache goes before the version on Windows while Logs goes after it; the Windows page explains this.

How It Works in One Paragraph

When platformdirs is imported, it picks one platform class (Android, Windows, MacOS, IOS or Unix) and exposes it as PlatformDirs. Every directory is a property on that class, computed fresh each time you read it: the class reads the relevant environment variables, joins appauthor (Windows only), appname and version onto a base folder, optionally creates the result, and returns it. The module-level functions such as user_data_dir() simply build a PlatformDirs object and read one property. Nothing is cached, so changes to environment variables are picked up immediately.

When to Use It, and When Not

Use platformdirs when:

  • your application or tool stores any file that is not part of the installed package: configuration, caches, databases, logs, sockets, lock files;
  • you want to save into, or read from, the user's own folders (Documents, Downloads, ...);
  • you need a cross-platform system service that should write to /var/lib, /var/log or C:\ProgramData.

It is not the right tool for:

  • the user's home directory itself: use pathlib.Path.home() or os.path.expanduser("~");
  • temporary files that must disappear: use the tempfile module (the runtime directory is for files that live as long as a session, such as sockets and PID files);
  • files shipped inside your package: use importlib.resources.

Project Status

platformdirs is classified as Production/Stable, is released under the MIT license, and is maintained under the tox-dev organization. It was introduced in 2021 as a friendly fork of the appdirs package, and well-known tools such as virtualenv, Black, Poetry and tox rely on it. It supports CPython 3.11 to 3.15 and PyPy.

Release notes are kept in the repository's changelog.

Where to Go Next

  • Installation: requirements and install commands.
  • Quick Start: a ten-minute tutorial from zero to a working app layout.
  • Choosing a Directory: which directory each kind of file belongs in.
  • Parameters: appname, appauthor, version, roaming, multipath, opinion, ensure_exists, use_site_for_root.
  • Path Reference Table: every directory on every platform, side by side.
  • Recipes: layered configuration, logging setup, cache cleanup, version migration and more.
  • Troubleshooting: surprising paths, warnings and errors explained.