Quick Start
This tutorial takes you from an empty Python shell to a small application that stores its settings, cache, database and logs in the right place on every operating system. Each step shows the output you should expect on Linux, macOS and Windows. Paths assume the defaults: no XDG_* variables set, user <user> on Linux and macOS, <User> on Windows.
1. Install platformdirs
pip install platformdirs
Confirm it imports:
python -c "import platformdirs; print(platformdirs.__version__)"
2. Ask for Your First Directory
Open a Python shell and request the user data directory for an application called MyApp, published by Acme:
>>> from platformdirs import user_data_dir
>>> user_data_dir("MyApp", "Acme")
The result depends on the platform:
| Platform | Result |
|---|---|
| Linux | '/home/<user>/.local/share/MyApp' |
| macOS | '/Users/<user>/Library/Application Support/MyApp' |
| Windows | 'C:\\Users\\<User>\\AppData\\Local\\Acme\\MyApp' |
The second argument, appauthor, is only used on Windows, where applications are grouped by publisher. If you leave it out on Windows, platformdirs reuses the app name, which produces a doubled ...\Local\MyApp\MyApp. Pass appauthor=False for a flat ...\Local\MyApp:
>>> user_data_dir("MyApp", appauthor=False) # on Windows
'C:\\Users\\<User>\\AppData\\Local\\MyApp'
3. Get a Path Instead of a String
Every function and property has a _path twin that returns a pathlib.Path:
>>> from platformdirs import user_cache_path
>>> user_cache_path("MyApp", "Acme")
PosixPath('/home/<user>/.cache/MyApp')
On Windows this returns WindowsPath('C:/Users/<User>/AppData/Local/Acme/MyApp/Cache'). The extra Cache folder is one of platformdirs' "opinionated" defaults; see opinion.
4. Use One Object for Several Directories
Calling a separate function for each directory repeats the arguments. Create a PlatformDirs object once instead:
>>> from platformdirs import PlatformDirs
>>> dirs = PlatformDirs("MyApp", "Acme")
>>> dirs.user_config_dir
'/home/<user>/.config/MyApp'
>>> dirs.user_log_dir
'/home/<user>/.local/state/MyApp/log'
>>> dirs.user_log_path
PosixPath('/home/<user>/.local/state/MyApp/log')
On macOS the same two properties return ~/Library/Application Support/MyApp and ~/Library/Logs/MyApp; on Windows, ...\AppData\Local\Acme\MyApp and ...\AppData\Local\Acme\MyApp\Logs.
5. Let platformdirs Create the Directories
Paths are only computed; nothing is created on disk by default. Pass ensure_exists=True and each property creates its directory (and missing parents) when you read it:
>>> dirs = PlatformDirs("MyApp", "Acme", ensure_exists=True)
>>> dirs.user_cache_path.is_dir()
True
On Linux and macOS, user directories created this way get mode 0700 (readable only by you), as the XDG specification asks.
6. Place and Find a Configuration File
place_config_file() returns the path of a file inside the user config directory and creates any missing directories on the way, even without ensure_exists. You write the file yourself:
>>> path = dirs.place_config_file("settings.toml")
>>> path.write_text('theme = "dark"\n', encoding="utf-8")
15
>>> dirs.find_config_file("settings.toml")
PosixPath('/home/<user>/.config/MyApp/settings.toml')
find_config_file() looks in the user config directory first and then in the system-wide ones (such as /etc/xdg/MyApp on Linux), and returns None if no copy exists. It never creates anything.
7. Put It Together
Here is a small application class that uses five directories:
import json
import logging
from platformdirs import PlatformDirs
class MyApp:
def __init__(self) -> None:
self.dirs = PlatformDirs("MyApp", "Acme", ensure_exists=True)
# Persistent data that must survive updates
self.db_path = self.dirs.user_data_path / "app.db"
# Regenerable data; deleting it must be harmless
self.thumbnails = self.dirs.user_cache_path / "thumbnails"
# Non-essential state such as window geometry
self.window_state = self.dirs.user_state_path / "window.json"
# Logs
logging.basicConfig(
filename=self.dirs.user_log_path / "app.log",
level=logging.INFO,
)
def load_settings(self) -> dict:
# User copy first, then system-wide defaults
if (path := self.dirs.find_config_file("settings.json")) is not None:
return json.loads(path.read_text(encoding="utf-8"))
return {"theme": "light"}
def save_settings(self, settings: dict) -> None:
path = self.dirs.place_config_file("settings.json")
path.write_text(json.dumps(settings), encoding="utf-8")
Run it:
>>> app = MyApp()
>>> app.save_settings({"theme": "dark"})
>>> app.load_settings()
{'theme': 'dark'}
8. See Every Directory at Once
To see what platformdirs returns on any machine, run the bundled report:
python -m platformdirs
It prints all 27 directories for a sample MyApp by MyCompany, in four variations. See Command Line Report.
What You Learned
user_*_dir()functions return astr;user_*_path()functions return aPath.PlatformDirs(appname, appauthor, ...)gives you every directory from one object.appauthormatters only on Windows;appauthor=Falseremoves the publisher folder.- Nothing is created unless you pass
ensure_exists=Trueor use aplace_*_file()method. find_*_file()searches the user directory first, then the site directories.
Next Steps
- Choosing a Directory: what belongs in data, config, cache, state, logs and runtime.
- Parameters:
version,roaming,opinion,multipathand the rest. - Recipes: layered configuration, cache cleanup, version migration.
- Testing Helpers: keep tests away from the real home directory.