Choosing a Directory
platformdirs exposes 27 directories. This page explains what each one is for, how app directories differ from media directories, and when to use the user_* versus the site_* variant, so you can decide where each of your files belongs.
- App directories vs media directories
- A decision guide
- The app directories
- The media and system directories
- User vs site directories
- Directories that share a location
App Directories vs Media Directories
The first question is: who owns the file?
- App directories hold files that belong to your application: databases, settings, caches, logs, sockets. They are scoped to your application: platformdirs appends
appname(andversion, and on Windowsappauthor) to the base folder. Users normally never browse them. - Media directories are the user's own folders, such as
~/Documentsor~/Downloads. They are not scoped to your app; the same path is returned whateverappnameyou pass. Use them only to save files the user asked for, or to read files from the user's library.
from platformdirs import user_data_path, user_documents_path
user_data_path("MyApp") # ~/.local/share/MyApp -> your app's private storage
user_documents_path() # ~/Documents -> the user's document library
If a file would confuse the user when they open the folder in their file manager, it belongs in an app directory.
A Decision Guide
For a file that belongs to the app:
- Can it be deleted without losing anything?
- It speeds things up and can be rebuilt (downloaded indexes, thumbnails, compiled templates) → cache.
- It only matters while the app or the login session runs (sockets, PID files, locks) → runtime.
- It is nice to keep but not essential (window positions, recent files, history) → state.
- Otherwise:
- It is a setting the user (or an admin) edits → config. On macOS,
.plist-style preferences can go to preference. - It is a log file → log.
- Everything else that must survive restarts and updates (databases, user content, downloaded assets) → data.
- It is a setting the user (or an admin) edits → config. On macOS,
For a file that belongs to the user, pick the folder matching the file type from the user's point of view: documents, downloads, pictures, videos, music, desktop, projects, public share, templates, or fonts.
The App Directories
| Kind | Functions / properties | Use for | Scoped to app | Has site_* |
|---|---|---|---|---|
| Data | user_data_dir, site_data_dir |
Databases, user content, downloaded assets | Yes | Yes |
| Config | user_config_dir, site_config_dir |
Settings files (TOML, INI, JSON, YAML) | Yes | Yes |
| Preference | user_preference_dir |
macOS preference files; alias of config elsewhere | Yes | No |
| Cache | user_cache_dir, site_cache_dir |
Regenerable data | Yes | Yes |
| State | user_state_dir, site_state_dir |
History, recent files, window layout | Yes | Yes |
| Log | user_log_dir, site_log_dir |
Log files | Yes | Yes |
| Runtime | user_runtime_dir, site_runtime_dir |
Sockets, PID files, locks | Yes | Yes |
Data
Files that must survive application restarts and upgrades. On Linux this follows $XDG_DATA_HOME (~/.local/share); on macOS it is ~/Library/Application Support; on Windows it is AppData\Local (or AppData\Roaming with roaming=True).
Config
Settings. On Linux, ~/.config ($XDG_CONFIG_HOME). On macOS and Windows the config directory is the same folder as the data directory, so if you need to keep both apart there, use subfolders (config/, db/).
Preference
Meaningful only on macOS, where Apple separates ~/Library/Application Support/MyApp (data) from ~/Library/Preferences/MyApp (preferences). On Linux, Windows, Android and iOS it returns the config directory. On iOS this is deliberate: Apple reserves Library/Preferences for NSUserDefaults. For most cross-platform programs, user_config_dir is the better choice.
Cache
Data your program can rebuild. Users and cleanup tools may delete it at any time, so always handle a missing cache. On iOS, Library/Caches is excluded from backups.
State
Data worth keeping between runs but not essential: undo history, recently opened files, window geometry. On Linux this is ~/.local/state ($XDG_STATE_HOME). On macOS, Windows, Android and iOS it is the same as the data directory.
Log
Log files. Note that the location differs considerably per platform: ~/.local/state/MyApp/log on Linux (inside the state directory), ~/Library/Logs/MyApp on macOS, ...\MyApp\Logs on Windows, and .../cache/MyApp/log on Android (inside the cache, so the system may clear it).
Runtime
Files that only make sense while the user is logged in or the app is running. On Linux this is $XDG_RUNTIME_DIR (typically /run/user/<uid>), a private, memory-backed directory that is emptied at logout. platformdirs validates it carefully; see Linux and Unix. On macOS it is ~/Library/Caches/TemporaryItems, on Windows AppData\Local\Temp, on iOS ~/tmp.
The Media and System Directories
None of these append appname or version.
| Function / property | Folder | Linux override |
|---|---|---|
user_documents_dir |
Documents | XDG_DOCUMENTS_DIR |
user_downloads_dir |
Downloads | XDG_DOWNLOAD_DIR |
user_pictures_dir |
Pictures | XDG_PICTURES_DIR |
user_videos_dir |
Videos (~/Movies on macOS) |
XDG_VIDEOS_DIR |
user_music_dir |
Music | XDG_MUSIC_DIR |
user_desktop_dir |
Desktop | XDG_DESKTOP_DIR |
user_projects_dir |
Projects | XDG_PROJECTS_DIR |
user_publicshare_dir |
Public (machine-wide C:\Users\Public on Windows) |
XDG_PUBLICSHARE_DIR |
user_templates_dir |
Templates for "New file" menus | XDG_TEMPLATES_DIR |
user_fonts_dir |
Per-user font installation folder | $XDG_DATA_HOME/fonts |
user_bin_dir, site_bin_dir |
Executables and scripts | none |
user_applications_dir, site_applications_dir |
.desktop files, app bundles, Start Menu shortcuts |
$XDG_DATA_HOME/applications, $XDG_DATA_DIRS |
A few points worth knowing:
- Fonts: copy a font into
user_fonts_dirto install it for the current user. On Windows the font also has to be registered (for example with the Win32AddFontResourceWfunction) before running programs can use it. - Bin:
user_bin_diris~/.local/binon Linux and macOS and%LOCALAPPDATA%\Programson Windows; whether it is onPATHdepends on the user's setup. - Applications:
user_applications_diris where launchers live (~/.local/share/applicationson Linux,~/Applicationson macOS, the Start MenuProgramsfolder on Windows). - Home directory: there is no property for the home folder itself; use
pathlib.Path.home().
User vs Site Directories
Each app directory exists in two flavors:
user_* |
site_* |
|
|---|---|---|
| Scope | One user | All users of the machine |
| Writable by | That user | Usually only root or an administrator |
| Typical content | The user's own settings and data | Defaults shipped by a package or set by an administrator, shared data |
| Linux example (config) | ~/.config/MyApp |
/etc/xdg/MyApp |
| Windows example (data) | C:\Users\<User>\AppData\Local\Acme\MyApp |
C:\ProgramData\Acme\MyApp |
Use user_* by default. Use site_* to read system-wide defaults, or when your program is a system service or installer running with administrative rights. The iterators and find_* helpers combine both: they yield the user directory first and then every site directory.
For daemons that run as root on Linux and should use /var/lib, /var/log and so on instead of /root/..., see use_site_for_root.
Directories That Share a Location
Several directories resolve to the same folder on some platforms. Plan your file names so that they do not collide:
| Platform | Same folder |
|---|---|
| macOS | user_data_dir, user_config_dir, user_state_dir (~/Library/Application Support/MyApp) |
| Windows | user_data_dir, user_config_dir, user_state_dir, user_preference_dir |
| Windows | site_data_dir, site_config_dir, site_state_dir |
| Windows, macOS | site_runtime_dir is user_runtime_dir |
| Android | user_data_dir, user_state_dir, user_applications_dir, and every site_* is its user_* |
| iOS | user_data_dir, user_config_dir, user_state_dir, user_preference_dir, and every site_* is its user_* |