Directory Creation and Safety
platformdirs can create the directories it returns, and on POSIX systems it checks the existing path before it does. These checks stop another local user from steering your files into a directory they control, for example by pre-creating /tmp/runtime-1000 or by planting a symlink in a world-writable folder. This page explains when directories are created, with which permissions, which checks run, and the errors you can get.
When Directories Are Created
platformdirs never creates anything unless you ask:
| Trigger | What is created |
|---|---|
ensure_exists=True, then reading a *_dir or *_path property |
That directory and its missing parents, on every read |
ensure_exists=True, then iterating iter_*_dirs() / iter_*_paths() |
Each directory as it is yielded |
place_*_file(name) (any ensure_exists) |
The user directory and every missing parent of name |
find_*_file(name) |
Nothing, ever |
Some directories are never created, even with ensure_exists=True: user_fonts_dir, user_bin_dir, site_bin_dir, user_applications_dir and site_applications_dir (except on Android, where the applications directory is the data directory), and media directories at their default location. A media directory is created only on Linux or macOS when its location was configured through an XDG_*_DIR variable or user-dirs.dirs; if that location is a dangling symlink (an unmounted drive, for instance), it is returned unchanged instead of raising.
Permissions of New Directories
| Directory | Mode requested | Platforms |
|---|---|---|
| User app directories (data, config, cache, state, log, runtime, preference) | 0700 |
Linux, macOS, iOS, Android |
Directories created by place_*_file |
0700 |
all (no real effect on Windows) |
| Configured media directories | 0775 |
Linux, macOS |
| Site directories, as a normal user | 0777 |
Linux, macOS |
| Site directories, as root | 0755 |
Linux and other Unix |
| Anything on Windows | Windows ACLs (the POSIX mode has no real effect) | Windows |
The process umask still applies, so a 0777 request with the common umask 022 yields 0755. Only directories that platformdirs creates get these modes; an existing directory keeps its permissions. The exception is the Unix temporary runtime fallback (/tmp/runtime-<uid>), which is reset to 0700 when ensure_exists=True.
The 0700 mode for user directories follows the XDG Base Directory Specification, which asks for directories readable only by their owner.
Path Checks on POSIX
Before creating a user directory on Linux, macOS, iOS and Android, or a site directory as root on Linux and other Unix systems, platformdirs walks the path from / down to the deepest part that already exists, using lstat so that it sees symlinks themselves. Symlinks are followed and their targets checked as well, up to 40 links. For each component it checks the owner and the mode, with rules that depend on what is being created:
| Policy | Used for | Allowed owners of existing components | Rejected modes |
|---|---|---|---|
| user | ensure_exists / place_*_file for user directories |
root or you; another owner only in a parent that others cannot write to, or the same owner as its parent | world-writable without the sticky bit |
| runtime | XDG_RUNTIME_DIR, /run/user/<uid>, the temporary runtime fallback |
root or you | group- or world-writable without the sticky bit |
| config | the folder holding user-dirs.dirs |
root or you | world-writable without the sticky bit |
| site | root creating site directories (ensure_exists, place_*_file with use_site_for_root) |
root only | world-writable without the sticky bit |
| site lookup | root reading an existing site directory with ensure_exists |
as for user, but only root counts as trusted | world-writable without the sticky bit |
In the user policy, a foreign-owned entry is never accepted if it is a symlink, or a shared (group- or world-writable) directory with the sticky bit, because the owner of such a directory can rename even your entries in it.
Each component must also be a directory (after following symlinks). Then platformdirs creates the missing components one by one with the mode from the previous section, and checks each new one: it must be owned by root or you (only root for site), and its mode must pass the same writability rule. If another process won the race and created it first with the wrong owner, creation fails.
Site directories created by a normal (non-root) user, and all site directories on macOS, are not checked; they are created the way os.makedirs would create them.
Group-Writable Directories
Group-writable directories are common on Linux (for example /var/log at mode 0775, or a shared project folder). The user and site policies accept them; only runtime rejects them, because the XDG specification requires that runtime directories be private.
Error Messages
All failures raise PermissionError with the offending path. Illustrative examples:
PermissionError: /srv/shared/cache, owned by uid 1001
PermissionError: /srv/drop, writable by another user
PermissionError: /home/<user>/.cache, which is not a directory
PermissionError: /home/<user>/loop, which has too many symbolic links
PermissionError: /var/cache/superapp is not a trusted directory
The first three come from the walk over existing components, the fourth when more than 40 symlinks are followed, and the last when a directory platformdirs just created (or found created by someone else) is not owned by a trusted user. A component that cannot be inspected at all produces <path> cannot be checked: <reason>.
Windows
Windows has no POSIX ownership or mode bits, so none of these checks run. Directories are created with the default ACLs inherited from their parent. Note that every user can create folders under C:\ProgramData, so treat existing site directories there as untrusted.
macOS: The Shared /Library/Caches
macOS ships /Library/Caches world-writable with the sticky bit, like /tmp. With ensure_exists=True, site_cache_dir creates or finds its entry there and then checks it, raising for example:
PermissionError: site cache directory /Library/Caches/SuperApp is owned by uid 502, not root or 501
unless it is a real directory owned by root or the current user. See macOS.
Runtime Directories
The runtime directory gets the strictest treatment, because it holds sockets and lock files. Its checks run on every read, not just when creating:
XDG_RUNTIME_DIRmust be a real directory, owned by you, with mode exactly0700, below parents that pass the runtime policy. Otherwise platformdirs warns and falls back.- The temporary fallback must not be a symlink, must be a directory, and must belong to you; otherwise reading
user_runtime_dirraisesPermissionError: runtime directory ...; set XDG_RUNTIME_DIR instead. place_runtime_file()creates the runtime directory before checking it, so another user cannot slip in between a check and the creation.
The details are on the Linux and Unix page.
Root and use_site_for_root
When root uses use_site_for_root=True, user_* properties resolve to site paths. Looking up an existing site directory owned by a service account is fine, but creating one, or placing a file, requires every path component to be owned by root, and new directories get mode 0755. This prevents a service account from redirecting root's writes. See Running as Root.
Unknown Home Directory
If the home directory cannot be determined, an unexpanded path such as ~/.config/SuperApp would be created relative to the current directory. platformdirs refuses instead:
RuntimeError: could not determine the home directory, refusing to create '~/.config/SuperApp'
In practice the property itself usually fails first with could not determine the home directory for '~/...'. See Troubleshooting.
Missing Drives and Shares
If a directory lies on a drive or network share that does not exist, creation stops at the missing root and the underlying OSError (typically FileNotFoundError) is raised.