ddn.os.path
Utilities for file and directory path manipulation. Heavily inspired by the Python's pathlib.
This module provides functions for joining, splitting, normalizing, traversing, and querying filesystem paths in a platform-independent manner. It builds upon and extends standard D path and file utilities, offering additional convenience functions for robust path handling.
Types 4
Cross-platform Path object inspired by Python's pathlib. Supports Windows, Linux, and POSIX environments.
private string _pathThe underlying path string.void symlinkTo(string target) constCreate a symbolic link at this path pointing to the given target.Path withSuffix(string newSuffix) const @safe Return a new Path with the last suffix (extension) replaced. Modeled after pathlib's `with_suffix`: - `newSuffix` must be empty (to remove the last suffix) or start with a dot (e.g., ".md"). -...Path withStem(string newStem) const @safeReturn a new Path with the stem (base name without its last suffix) replaced, preserving the original suffixes.string[] suffixes() const @safeReturn all suffixes (extensions) as an array, each including the leading dot.bool sameOpenFile(int fd1, int fd2) staticReturns `true` if two open files refer to the same underlying file object.bool sameOpenFile(File f1, File f2) staticReturns `true` if two `std.stdio.File` handles refer to the same file.void lchown(int uid, int gid) constLike `chown` but does not follow symlinks (operates on the link itself).Path joinpath(T...)(T args) if (args.length > 0) const @safeJoin this path with one or more path segments.Path expandUser() constExpands a leading tilde in the path to the user's home directory, similar to Python's `os.path.expanduser` / `pathlib.Path.expanduser` semantics.string _expandDollarVars(string s) staticExpand `$NAME` and `${NAME}` references using the process environment.Path opBinary(string op : "/")(string other) const @safeOperator overload for joining paths using the ~ operator.Path opBinary(string op : "/")(Path other) const @safeOperator overload to join with another `Path` using `/`.bool isRelativeTo(Path other) const @safeReturn whether this path is relative to `other` (pathlib-like).Path relativeTo(Path other) const @safeCompute the relative path from `other` to `this` (pathlib-like).void rename(string target) constRenames or moves the file or directory represented by this `Path` to `target`.void replace(string target) constOverwrites `target` by moving this path over it. On platforms that support it (POSIX), this is an atomic replacement using `rename(2)`.Path normalize() const @safeReturns a normalized version of this path where redundant separators are collapsed and dot-segments (`.` and `..`) are simplified in a platform-appropriate way.bool isReservedDeviceName() constDetect whether this path's final component is a reserved Windows device name.string readText(string encoding = "utf-8", string errors = "strict", string newline = null) constReads the contents of the file as text with configurable decoding and newline handling, inspired by Python's `pathlib.Path.read_text`.void writeText(string content, string encoding = "utf-8", string errors = "strict",
string newline = null, bool atomic = false) constWrites text to the file (overwrites) with configurable encoding, newline handling, and optional atomic replace. Modeled after Python's `pathlib.Path.write_text`.void writeBytes(const(ubyte)[] content, bool atomic = false) constWrites bytes to the file (overwrites). Supports optional atomic replacement.auto iterdir() const Returns a lazy input range of `Path` values for each immediate entry in this directory, similar to Python's `Path.iterdir()`. Notes: - The range yields children in unspecified order (as provid...auto walk(bool topDown = true, bool followSymlinks = false,
bool delegate(const Path) dirFilter = null) constRecursively traverses the directory tree rooted at `this` and yields `WalkEntry` values describing each directory and its immediate children.void utime(SysTime atime, SysTime mtime) constUpdate file timestamps (like POSIX `utime`), following symlinks.void touch(bool existOk = true, Nullable!uint mode = Nullable!uint.init,
Nullable!FileTimes times = Nullable!FileTimes.init) constCreate the file and/or update its timestamps, pathlib-like.Path resolve(bool strict = false) constResolves this path similarly to Python's `Path.resolve(strict=False)`: - Returns an absolute path with symlinks expanded where possible. - Detects symlink loops and throws an exception. - When `str...bool opEquals(const Path rhs) const @safeChecks equality with another `Path` using normalized, platform-aware semantics.int opCmp(const Path rhs) const @safeCompares this `Path` to another using normalized, platform-aware rules.this(string path)Construct a Path from a string.WalkEntryA single walk step for `walk()`: the current root directory with its child directories and files.FileTimesRepresents file timestamps for access and modification time.Base class for all path-related exceptions thrown by this module.
The error model maps common OS error codes to a small hierarchy of exceptions for consistency across APIs:
NotFoundErrorwhen a path component is missing (ENOENT/ENOTDIR).PermissionErrorwhen access is denied (EACCES/EPERM).PathErrorfor other filesystem-related failures.
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)Create a `PathError` with a message and optional chained cause.Raised when the specified path or one of its components does not exist.
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)Raised when the operation lacks sufficient privileges or access is denied.
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)Functions 4
Exception _mapErrnoToPathError(string op, string p, int err, Throwable next,
string file = __FILE__, size_t line = __LINE__)Map an errno value to the PathError hierarchy.string windowsDirIdentity(string d)Canonical directory identity on Windows for symlink-loop protection.void _rethrowMapped(string op, string p, Throwable ex,
string file = __FILE__, size_t line = __LINE__)Re-throw a filesystem exception mapped into the PathError hierarchy.bool sameFilesystem(string path1, string path2)Check whether two paths live on the same filesystem.