Skip to content

Commit 8a9378c

Browse files
committed
📝 docs: document user media dirs, fonts, and preference dir
Add explanation, per-platform tables, and API references for user_publicshare_dir, user_templates_dir, user_fonts_dir, and user_preference_dir. Also add the missing "User media directories" section to explanation.rst covering the full set of non-app-scoped dirs, with upstream spec links (XDG user-dirs, Apple File System Guide, Windows KNOWNFOLDERID). Notable callouts: macOS has no native templates dir (~/Templates fallback), Windows publicshare is machine-wide C:\Users\Public, fonts on Windows uses the Win10+ per-user location, and preference_dir is only distinct from config_dir on macOS.
1 parent c538533 commit 8a9378c

3 files changed

Lines changed: 295 additions & 0 deletions

File tree

docs/api.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,42 @@ See also: :ref:`platforms:``user_projects_dir```
150150

151151
.. autofunction:: platformdirs.user_projects_path
152152

153+
User public share directory
154+
===========================
155+
156+
See also: :ref:`platforms:``user_publicshare_dir```
157+
158+
.. autofunction:: platformdirs.user_publicshare_dir
159+
160+
.. autofunction:: platformdirs.user_publicshare_path
161+
162+
User templates directory
163+
========================
164+
165+
See also: :ref:`platforms:``user_templates_dir```
166+
167+
.. autofunction:: platformdirs.user_templates_dir
168+
169+
.. autofunction:: platformdirs.user_templates_path
170+
171+
User fonts directory
172+
====================
173+
174+
See also: :ref:`platforms:``user_fonts_dir```
175+
176+
.. autofunction:: platformdirs.user_fonts_dir
177+
178+
.. autofunction:: platformdirs.user_fonts_path
179+
180+
User preference directory
181+
=========================
182+
183+
See also: :ref:`platforms:``user_preference_dir```
184+
185+
.. autofunction:: platformdirs.user_preference_dir
186+
187+
.. autofunction:: platformdirs.user_preference_path
188+
153189
********************
154190
Shared directories
155191
********************

docs/explanation.rst

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,133 @@ Use ``user_log_dir`` and ``site_log_dir`` for application logs:
141141
format="%(asctime)s - %(levelname)s - %(message)s",
142142
)
143143
144+
******************************
145+
User media directories
146+
******************************
147+
148+
Unlike app dirs (data, config, cache, etc.), media dirs are **not** scoped to the app name. They point to
149+
standard user-facing folders that exist independently of any particular application. Use them when your app
150+
needs to read from or save into a folder the user already expects — not when storing application state.
151+
152+
The distinction matters:
153+
154+
- ``user_data_dir("MyApp")`` → ``~/.local/share/MyApp`` — your app's private storage
155+
- ``user_documents_dir()`` → ``~/Documents`` — the user's document library
156+
157+
On Linux, media dirs are defined by the `XDG user-dirs specification
158+
<https://www.freedesktop.org/wiki/Software/xdg-user-dirs/>`_ and stored in ``~/.config/user-dirs.dirs``.
159+
The ``xdg-user-dirs`` tool lets users relocate them. Set the corresponding environment variable
160+
(``XDG_DOCUMENTS_DIR``, ``XDG_DOWNLOAD_DIR``, etc.) to override on a per-session basis. On macOS and
161+
Windows, ``platformdirs`` returns the platform-conventional location.
162+
163+
Documents, downloads, and media
164+
================================
165+
166+
Use these dirs when saving or opening files that belong in the user's library:
167+
168+
.. list-table::
169+
:widths: 30 20 50
170+
:header-rows: 1
171+
172+
* - Property
173+
- XDG variable
174+
- Typical use
175+
* - ``user_documents_dir``
176+
- ``XDG_DOCUMENTS_DIR``
177+
- Exported reports, user-authored files
178+
* - ``user_downloads_dir``
179+
- ``XDG_DOWNLOAD_DIR``
180+
- Files fetched from the internet at user request
181+
* - ``user_pictures_dir``
182+
- ``XDG_PICTURES_DIR``
183+
- Images the user owns
184+
* - ``user_videos_dir``
185+
- ``XDG_VIDEOS_DIR``
186+
- Video files the user owns
187+
* - ``user_music_dir``
188+
- ``XDG_MUSIC_DIR``
189+
- Audio files the user owns
190+
191+
.. code-block:: python
192+
193+
from platformdirs import user_documents_path
194+
195+
# Save an exported report where the user expects documents
196+
report = user_documents_path() / "report.pdf"
197+
198+
Do not use ``user_documents_dir`` to store application data or config. If the file would confuse the user if
199+
they opened the folder, it belongs in ``user_data_dir`` instead.
200+
201+
Desktop, projects, and public share
202+
=====================================
203+
204+
.. list-table::
205+
:widths: 30 20 50
206+
:header-rows: 1
207+
208+
* - Property
209+
- XDG variable
210+
- Typical use
211+
* - ``user_desktop_dir``
212+
- ``XDG_DESKTOP_DIR``
213+
- Shortcut files, launchers (rarely needed in code)
214+
* - ``user_projects_dir``
215+
- ``XDG_PROJECTS_DIR``
216+
- Root directory for user's coding projects (`recently added to xdg-user-dirs
217+
<https://gitlab.freedesktop.org/xdg/xdg-user-dirs/-/commit/217cae71c620ed2b3ed2936256ece68defccc6ab>`_)
218+
* - ``user_publicshare_dir``
219+
- ``XDG_PUBLICSHARE_DIR``
220+
- Files shared with other users on the same machine
221+
222+
On Windows, ``user_publicshare_dir`` returns ``C:\Users\Public`` (``%PUBLIC%``) — a machine-wide shared
223+
folder, not per-user. This matches what Rust ``dirs`` and Java ``directories-jvm`` return, following the
224+
platform convention.
225+
226+
Templates
227+
=========
228+
229+
``user_templates_dir`` (``XDG_TEMPLATES_DIR``) points to the folder used by file managers for new-file
230+
templates. macOS has no platform-defined templates directory; ``~/Templates`` is returned as a pragmatic
231+
fallback.
232+
233+
Fonts
234+
=====
235+
236+
``user_fonts_dir`` points to the per-user font installation directory:
237+
238+
- **Linux**: ``$XDG_DATA_HOME/fonts`` (default ``~/.local/share/fonts``) — derived from ``$XDG_DATA_HOME``,
239+
not a dedicated env var. See the `XDG Base Directory Specification
240+
<https://specifications.freedesktop.org/basedir/latest/>`_.
241+
- **macOS**: ``~/Library/Fonts``
242+
- **Windows**: ``%LOCALAPPDATA%\Microsoft\Windows\Fonts`` — the per-user font location added in Windows 10
243+
244+
.. code-block:: python
245+
246+
import shutil
247+
from platformdirs import user_fonts_path
248+
249+
font_dir = user_fonts_path()
250+
font_dir.mkdir(parents=True, exist_ok=True)
251+
shutil.copy("MyFont.ttf", font_dir / "MyFont.ttf")
252+
253+
**********************
254+
Preference directory
255+
**********************
256+
257+
``user_preference_dir`` is meaningful mainly on macOS, where Apple's conventions distinguish two separate
258+
locations:
259+
260+
- ``~/Library/Application Support/AppName`` — long-term application data, databases, plug-ins
261+
- ``~/Library/Preferences/AppName`` — user-adjustable preference files (historically ``.plist``)
262+
263+
On Linux and Windows, ``user_preference_dir`` is an alias for ``user_config_dir`` — the XDG and Windows
264+
conventions make no such distinction. On Android, it also aliases ``user_config_dir``.
265+
266+
Use ``user_preference_dir`` when you specifically need to follow Apple's `File System Programming Guide
267+
<https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html>`_
268+
and store preference files in ``~/Library/Preferences``. For most cross-platform applications
269+
``user_config_dir`` is sufficient.
270+
144271
**************************
145272
User vs site directories
146273
**************************

docs/platforms.rst

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,9 @@ See also: :ref:`api:User desktop directory`
406406

407407
See also: :ref:`api:User projects directory`
408408

409+
Defined by `$XDG_PROJECTS_DIR <https://gitlab.freedesktop.org/xdg/xdg-user-dirs/-/commit/217cae71c620ed2b3ed2936256ece68defccc6ab>`_
410+
(recently added to xdg-user-dirs).
411+
409412
.. tab-set::
410413

411414
.. tab-item:: Linux
@@ -429,6 +432,135 @@ See also: :ref:`api:User projects directory`
429432

430433
``/storage/emulated/0/Projects``
431434

435+
``user_publicshare_dir``
436+
========================
437+
438+
See also: :ref:`api:User public share directory`
439+
440+
Defined by `$XDG_PUBLICSHARE_DIR <https://www.freedesktop.org/wiki/Software/xdg-user-dirs/>`_.
441+
442+
On Windows, this is the machine-wide ``C:\Users\Public`` (``%PUBLIC%``), shared across all local accounts —
443+
not a per-user directory. See `FOLDERID_Public
444+
<https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid>`_.
445+
446+
.. tab-set::
447+
448+
.. tab-item:: Linux
449+
:sync: linux
450+
451+
``~/Public`` (from ``$XDG_PUBLICSHARE_DIR`` if set, else ``$XDG_PUBLICSHARE_DIR`` entry in
452+
``user-dirs.dirs``, else ``~/Public``)
453+
454+
.. tab-item:: macOS
455+
:sync: macos
456+
457+
``~/Public``
458+
459+
.. tab-item:: Windows
460+
:sync: windows
461+
462+
``C:\Users\Public`` (``%PUBLIC%``)
463+
464+
.. tab-item:: Android
465+
:sync: android
466+
467+
``/storage/emulated/0/Public``
468+
469+
``user_templates_dir``
470+
======================
471+
472+
See also: :ref:`api:User templates directory`
473+
474+
Defined by `$XDG_TEMPLATES_DIR <https://www.freedesktop.org/wiki/Software/xdg-user-dirs/>`_. macOS has no
475+
platform-defined templates directory; ``~/Templates`` is returned as a pragmatic fallback. On Windows, see
476+
`FOLDERID_Templates <https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid>`_.
477+
478+
.. tab-set::
479+
480+
.. tab-item:: Linux
481+
:sync: linux
482+
483+
``~/Templates`` (from ``$XDG_TEMPLATES_DIR`` if set, else ``$XDG_TEMPLATES_DIR`` entry in
484+
``user-dirs.dirs``, else ``~/Templates``)
485+
486+
.. tab-item:: macOS
487+
:sync: macos
488+
489+
``~/Templates`` (pragmatic fallback; macOS has no native templates directory)
490+
491+
.. tab-item:: Windows
492+
:sync: windows
493+
494+
``%APPDATA%\Microsoft\Windows\Templates``
495+
496+
.. tab-item:: Android
497+
:sync: android
498+
499+
``/storage/emulated/0/Templates``
500+
501+
``user_fonts_dir``
502+
==================
503+
504+
See also: :ref:`api:User fonts directory`
505+
506+
Derived from ``$XDG_DATA_HOME/fonts`` on Linux (no dedicated env var). See the `XDG Base Directory
507+
Specification <https://specifications.freedesktop.org/basedir/latest/>`_. On Windows, uses the per-user font
508+
location added in Windows 10.
509+
510+
.. tab-set::
511+
512+
.. tab-item:: Linux
513+
:sync: linux
514+
515+
``$XDG_DATA_HOME/fonts`` (default ``~/.local/share/fonts``)
516+
517+
.. tab-item:: macOS
518+
:sync: macos
519+
520+
``~/Library/Fonts``
521+
522+
.. tab-item:: Windows
523+
:sync: windows
524+
525+
``%LOCALAPPDATA%\Microsoft\Windows\Fonts``
526+
527+
.. tab-item:: Android
528+
:sync: android
529+
530+
``/storage/emulated/0/fonts``
531+
532+
``user_preference_dir``
533+
=======================
534+
535+
See also: :ref:`api:User preference directory`
536+
537+
On macOS, ``~/Library/Preferences`` is distinct from ``~/Library/Application Support`` (``user_config_dir``).
538+
See `Apple's File System Programming Guide
539+
<https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html>`_.
540+
On all other platforms, this aliases ``user_config_dir``.
541+
542+
.. tab-set::
543+
544+
.. tab-item:: Linux
545+
:sync: linux
546+
547+
Same as ``user_config_dir`` (``$XDG_CONFIG_HOME`` or ``~/.config/AppName``)
548+
549+
.. tab-item:: macOS
550+
:sync: macos
551+
552+
``~/Library/Preferences/AppName`` (distinct from ``~/Library/Application Support``)
553+
554+
.. tab-item:: Windows
555+
:sync: windows
556+
557+
Same as ``user_config_dir`` (``%APPDATA%\AppName``)
558+
559+
.. tab-item:: Android
560+
:sync: android
561+
562+
Same as ``user_config_dir``
563+
432564
********************
433565
Shared directories
434566
********************

0 commit comments

Comments
 (0)