Parameters
PlatformDirs and the module-level functions accept the same eight parameters. This page documents each one: type, default, which platforms honor it, and the exact effect on the returned paths, including edge cases found in the source.
- Quick reference
appnameappauthorversionroamingmultipathopinionensure_existsuse_site_for_root- Path validation of names
Quick Reference
PlatformDirs(
appname=None,
appauthor=None,
version=None,
roaming=False,
multipath=False,
opinion=True,
ensure_exists=False,
use_site_for_root=False,
)
All eight can be passed positionally to PlatformDirs in this order, but keywords are clearer. They are stored as plain attributes, so you can change them on an existing object (dirs.version = "2.0"); every property is recomputed on each read.
| Parameter | Type | Default | Honored on |
|---|---|---|---|
appname |
str \| None |
None |
All platforms |
appauthor |
str \| Literal[False] \| None |
None |
Windows only |
version |
str \| None |
None |
All platforms |
roaming |
bool |
False |
Windows only |
multipath |
bool |
False |
Linux/Unix, macOS |
opinion |
bool |
True |
Linux/Unix, Windows, Android |
ensure_exists |
bool |
False |
All platforms |
use_site_for_root |
bool |
False |
Linux/Unix only |
The module-level functions accept only the parameters that matter for their directory; see Module Functions for each signature.
appname
| Property | Value |
|---|---|
| Type | str \| None |
| Default | None |
| Affects | All app directories (data, config, cache, state, log, runtime, preference) |
The name of your application. It becomes the last folder of every app directory (before version). With None (or an empty string) you get the bare base directory, which is rarely what you want for writing but useful for discovering the base:
>>> from platformdirs import user_data_dir
>>> user_data_dir("SuperApp") # Linux
'/home/<user>/.local/share/SuperApp'
>>> user_data_dir()
'/home/<user>/.local/share'
appname is never added to media, fonts, bin or applications directories. One exception exists on Android, where user_applications_dir is the data directory and therefore does include it.
Note: Because
appnameis a folder name, avoid characters your target file systems reject (:on Windows, for example). Names that would escape the base directory are rejected; see Path validation of names.
appauthor
| Property | Value |
|---|---|
| Type | str \| Literal[False] \| None |
| Default | None (falls back to appname) |
| Affects | Windows app directories only; ignored everywhere else |
The publisher or organization. On Windows, app directories have the layout <base>\<appauthor>\<appname>:
>>> user_data_dir("SuperApp", "Acme") # Windows
'C:\\Users\\<User>\\AppData\\Local\\Acme\\SuperApp'
>>> user_data_dir("SuperApp") # appauthor=None falls back to appname
'C:\\Users\\<User>\\AppData\\Local\\SuperApp\\SuperApp'
>>> user_data_dir("SuperApp", False) # no author folder
'C:\\Users\\<User>\\AppData\\Local\\SuperApp'
Details from the Windows backend:
- The author folder is only added when
appnameis set.user_data_dir(None, "Acme")returns the bareAppData\Local. - An empty string behaves like
None: it falls back toappname. - Only
False(the boolean) suppresses the folder.
Pick an explicit author when you publish several applications under one organization; pick False for a flat layout. Many tools pass appauthor=False for that reason.
version
| Property | Value |
|---|---|
| Type | str \| None |
| Default | None |
| Affects | All app directories; only when appname is also set |
Appends a version folder so several versions of your application can run side by side without sharing files:
>>> from platformdirs import PlatformDirs
>>> dirs = PlatformDirs("SuperApp", "Acme", version="1.0")
>>> dirs.user_data_dir # macOS
'/Users/<user>/Library/Application Support/SuperApp/1.0'
>>> dirs.user_cache_dir # macOS
'/Users/<user>/Library/Caches/SuperApp/1.0'
The version is ignored when appname is not set. A typical value is "<major>.<minor>".
Where the version lands relative to the opinion subfolders differs by directory, which matters if you compute paths by hand:
| Directory | Layout with version="1.0" |
|---|---|
Windows user_cache_dir |
...\Acme\SuperApp\Cache\1.0 |
Windows user_log_dir |
...\Acme\SuperApp\1.0\Logs |
Linux user_log_dir |
~/.local/state/SuperApp/1.0/log |
Android user_log_dir |
.../cache/SuperApp/1.0/log |
Warning: Versioned config or data directories start empty for each new version. You are responsible for migrating files from the previous version; see Recipes.
roaming
| Property | Value |
|---|---|
| Type | bool |
| Default | False |
| Affects | Windows user_data_dir, and everything derived from it; ignored everywhere else |
On Windows, roaming=True uses the roaming profile folder AppData\Roaming (CSIDL_APPDATA) instead of AppData\Local (CSIDL_LOCAL_APPDATA). In a Windows domain with roaming profiles, files there follow the user from machine to machine.
>>> user_data_dir("SuperApp", "Acme", roaming=True) # Windows
'C:\\Users\\<User>\\AppData\\Roaming\\Acme\\SuperApp'
Because the Windows config, state, preference and log directories are derived from the data directory, roaming moves them too. It does not affect the cache or runtime directories, which always stay in AppData\Local.
The module functions that accept roaming are user_data_*, user_config_*, user_state_*, user_preference_* (keyword-only) and user_log_* (keyword-only).
multipath
| Property | Value |
|---|---|
| Type | bool |
| Default | False |
| Affects | site_data_dir, site_config_dir, site_applications_dir on Linux/Unix and macOS; site_cache_dir on macOS with Homebrew Python |
Site directories can be a list of folders: on Linux, XDG_DATA_DIRS defaults to /usr/local/share:/usr/share. By default the site_*_dir property returns only the first entry. With multipath=True it returns every entry joined with os.pathsep (:):
>>> from platformdirs import site_data_dir
>>> site_data_dir("SuperApp") # Linux
'/usr/local/share/SuperApp'
>>> site_data_dir("SuperApp", multipath=True)
'/usr/local/share/SuperApp:/usr/share/SuperApp'
Things to know:
- The
*_pathvariants (site_data_path,site_config_path,site_cache_path,site_applications_path) always return only the first entry, even withmultipath=True. - With
multipath=True,site_data_dir,site_config_dirand (macOS)site_cache_dirraiseValueErrorif any entry containsos.pathsep, because the joined string could not be split back. The message ismultipath cannot join site directories that contain ':', got [...]. This usually comes from anappnameorversioncontaining:.site_applications_dirperforms no such check. - With
ensure_exists=True, only the entries actually returned are created: one withmultipath=False, all of them withmultipath=True. - On Windows, Android and iOS,
multipathhas no effect. - To work with all entries as a list, prefer
iter_data_paths()/iter_config_paths(), which yield every site entry regardless ofmultipath.
opinion
| Property | Value |
|---|---|
| Type | bool |
| Default | True |
| Affects | Cache and log directories on Windows, log directory on Linux/Unix, log and runtime directories on Android |
With opinion=True, platformdirs adds a conventional subfolder to some directories that would otherwise coincide with another one:
| Platform | Directory | opinion=True |
opinion=False |
|---|---|---|---|
| Windows | user_cache_dir, site_cache_dir |
...\Acme\SuperApp\Cache |
...\Acme\SuperApp |
| Windows | user_log_dir, site_log_dir |
...\Acme\SuperApp\Logs |
...\Acme\SuperApp |
| Linux/Unix | user_log_dir |
~/.local/state/SuperApp/log |
~/.local/state/SuperApp |
| Android | user_log_dir, site_log_dir |
.../cache/SuperApp/log |
.../cache/SuperApp |
| Android | user_runtime_dir, site_runtime_dir |
.../cache/SuperApp/tmp |
.../cache/SuperApp |
>>> from platformdirs import user_cache_dir
>>> user_cache_dir("SuperApp", "Acme") # Windows
'C:\\Users\\<User>\\AppData\\Local\\Acme\\SuperApp\\Cache'
>>> user_cache_dir("SuperApp", "Acme", opinion=False)
'C:\\Users\\<User>\\AppData\\Local\\Acme\\SuperApp'
The Linux site_log_dir (/var/log/SuperApp) and all macOS and iOS directories ignore opinion. On Windows, Cache is only added when appname is set; Logs is added even without an app name.
The module functions accept opinion for user_cache_*, site_cache_*, user_log_*, site_log_*, user_runtime_* and site_runtime_*.
ensure_exists
| Property | Value |
|---|---|
| Type | bool |
| Default | False |
| Affects | Whether reading a property creates the directory |
With ensure_exists=True, reading a property creates the directory it returns, including missing parents, every time it is read (creating an existing directory is a no-op):
from platformdirs import PlatformDirs
dirs = PlatformDirs("SuperApp", "Acme", ensure_exists=True)
log_file = dirs.user_log_path / "app.log" # the log directory now exists
What gets created:
| Directory | Created? |
|---|---|
| Data, config, cache, state, log, runtime, preference (user and site) | Yes |
| Media directories (documents, downloads, ...) on Linux/Unix | Only if the user configured them through an XDG_*_DIR variable or user-dirs.dirs |
| Media directories on macOS | Only if set through an XDG_*_DIR environment variable |
Media directories on Windows, Android, iOS; platform defaults such as ~/Documents |
Never |
| Fonts, bin and applications directories | Never, because the desktop session or system owns them (except on Android, where user_applications_dir is the data directory) |
Permissions on POSIX systems (Linux, macOS, iOS, Android):
| Kind | Mode for each newly created directory |
|---|---|
user_* app directories, and each missing parent |
0700, as the XDG spec requires |
| Configured media directories | 0775 minus the umask |
site_* directories created by a normal user |
Default mode (0777) minus the umask |
site_* directories created by root on Linux/Unix |
0755 minus the umask, and only under root-owned parents |
Existing directories keep their permissions; the one exception is the Unix temporary runtime fallback, which ensure_exists repairs to 0700. On Windows, ensure_exists creates directories with the default permissions and performs no ownership checks.
Errors you may see:
RuntimeError: could not determine the home directory ...when no absolute home directory can be found.PermissionErrorwhen a parent fails the ownership and permission checks described in Directory Creation and Safety, for example a/Library/Caches/SuperAppcreated by another user on macOS.FileExistsErrorwhen a configured media directory is a regular file or a symlink to one. A dangling symlink is returned unchanged.- Any
OSErrorfrommkdir, such as a missing drive.
The *_path properties create their directory too, since they are built from the *_dir value. For multi-entry site directories they create only the first entry.
Note:
ensure_existsis also accepted (keyword-only) by the module-level media functions such asuser_documents_path(ensure_exists=True), but not byuser_fonts_*,user_bin_*orsite_bin_*, which are never created.
use_site_for_root
| Property | Value |
|---|---|
| Type | bool |
| Default | False (kept for backwards compatibility) |
| Affects | Linux/Unix only, and only when the effective user ID is 0 |
System services often run as root. Without this flag, root gets root's own user directories (/root/.local/share/...). With use_site_for_root=True and an effective UID of 0, the user_* properties return their site_* counterparts instead:
| Property | Redirected to |
|---|---|
user_data_dir / _path |
site_data_dir / site_data_path |
user_config_dir / _path, user_preference_path |
site_config_dir / site_config_path |
user_cache_dir |
site_cache_dir |
user_state_dir |
site_state_dir |
user_log_dir |
site_log_dir |
user_runtime_dir |
the site runtime default (/run/<app>, /var/run/<app> on BSD) |
user_bin_dir |
site_bin_dir |
user_applications_dir / _path |
site_applications_dir / site_applications_path |
from platformdirs import PlatformDirs
dirs = PlatformDirs("myservice", use_site_for_root=True)
dirs.user_data_dir # as root: /usr/local/share/myservice; otherwise ~/.local/share/myservice
While the redirect is active, XDG user variables such as XDG_DATA_HOME, XDG_RUNTIME_DIR, the XDG_*_DIR media variables and user-dirs.dirs are ignored for the redirected directories, because under sudo they usually belong to the invoking user. Media and fonts directories have no site equivalent: they return their default under an absolute HOME, or root's home from the password database if HOME is unset, empty or relative, and are never created.
Other effects:
- The iterators skip the user directory (it equals the site one).
place_*_file()places files in the site directory and requires root-owned parents.user_preference_dirstill returns the user config directory value throughuser_config_dir, which is redirected, so it also resolves to the site config directory.- The flag is accepted on every platform but ignored outside Linux/Unix. Inside
isolated_dirs()it is ignored too.
The effective UID is read with os.geteuid(); on platforms without it, such as Pyodide, os.getuid() is used.
Path Validation of Names
appname, appauthor and version are validated when set, both in the constructor and on later assignment. A value is rejected with ValueError if it would let the joined path escape the base directory:
- it starts with
/or\; - it has a drive (
C:,C:x,\\server\share); - one of its components is
...
>>> PlatformDirs("../evil")
ValueError: appname must stay inside the base directory, got '../evil'
>>> PlatformDirs("a", version="x/../../y")
ValueError: version must stay inside the base directory, got 'x/../../y'
Subfolders are allowed: PlatformDirs("Acme/SuperApp") simply nests two folders. Empty values and None skip validation. The same rules apply to the name argument of place_*_file() and find_*_file().