11Handling Paths
22==============
33
4- Historically, this chapter recommended the utilities ``syspath() ``,
5- ``normpath() ``, ``bytestring_path() ``, and ``displayable_path() `` for handling
6- file paths in Beets. These ensured consistent behavior across Linux, macOS, and
7- Windows before Python’s ``pathlib `` offered a unified and reliable API.
8-
9- - ``syspath() `` worked around Windows Unicode and long-path issues by converting
10- to a system-safe string (adding the ``\\?\ `` prefix where needed). Modern
11- Python (≥3.6) handles this automatically through its wide-character APIs.
12- - ``normpath() `` normalized slashes and removed ``./ `` or ``.. `` parts but did
13- not expand ``~ ``. It was used mainly for paths from user input or config
14- files.
15- - ``bytestring_path() `` converted paths to ``bytes `` for storage in the
16- database. Paths in the database are still stored as bytes today, though there
17- are plans to eventually store ``pathlib.Path `` objects directly.
18- - ``displayable_path() `` converted byte paths to Unicode for display or logging.
19-
20- These utilities remain safe to use when maintaining older code, but new code and
21- refactors should prefer ``pathlib.Path ``:
22-
23- - Use the ``.filepath `` property on ``Item `` and ``Album `` to access paths as
24- ``pathlib.Path ``. This replaces ``displayable_path(item.path) ``.
25- - Normalize or expand paths using ``Path(...).expanduser().resolve() ``, which
26- correctly expands ``~ `` and resolves symlinks.
27- - Cross-platform details like path separators, Unicode handling, and long-path
28- support are handled automatically by ``pathlib ``.
4+ ``pathlib `` provides a clean, cross-platform API for working with filesystem
5+ paths.
6+
7+ Use the ``.filepath `` property on ``Item `` and ``Album `` to access paths as
8+ ``pathlib.Path `` objects. This produces a readable, native representation
9+ suitable for printing, logging, or further processing.
10+
11+ Normalize paths using ``Path(...).expanduser().resolve() ``, which expands ``~ ``
12+ and resolves symlinks.
13+
14+ Cross-platform differences—such as path separators, Unicode handling, and
15+ long-path support (Windows) are automatically managed by ``pathlib ``.
16+
17+ When storing paths in the database though, convert them to bytes with
18+ ``bytestring_path() ``. Paths in Beets currently remain to be stored as bytes,
19+ though there are plans to eventually store ``pathlib.Path `` objects directly.
20+
21+ Legacy utilities
22+ ----------------
23+
24+ Historically, Beets used custom utilities to ensure consistent behavior across
25+ Linux, macOS, and Windows before ``pathlib `` became reliable:
26+
27+ - ``syspath() ``: worked around Windows Unicode and long-path limitations by
28+ converting to a system-safe string (adding the ``\\?\ `` prefix where needed).
29+ - ``normpath() ``: normalized slashes and removed ``./ `` or ``.. `` parts but did
30+ not expand ``~ ``.
31+ - ``bytestring_path() ``: converted paths to bytes for database storage (still
32+ used for that purpose today).
33+ - ``displayable_path() ``: converted byte paths to Unicode for display or
34+ logging.
35+
36+ These functions remain safe to use in legacy code, but new code should rely
37+ solely on ``pathlib.Path ``.
2938
3039Examples
3140--------
@@ -50,9 +59,4 @@ When storing paths in the database
5059
5160.. code-block :: python
5261
53- path_bytes = bytestring_path(item.filepath)
54-
55- In short, the old utilities were necessary for cross-platform safety in early
56- Beets versions, but ``pathlib.Path `` now provides these guarantees natively and
57- should be used for all new code. ``bytestring_path() `` is still used only for
58- database storage.
62+ path_bytes = bytestring_path(Path(" /some/path/to/file.mp3" ))
0 commit comments