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 @safeReturn a new Path with the last suffix (extension) replaced.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 exists() const @safeReturns true if the path exists Checks if the path exists in the filesystem.bool sameOpenFile(int fd1, int fd2)Returns `true` if two open files refer to the same underlying file object.bool sameOpenFile(File f1, File f2)Returns `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)string _getEnv(string name)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.Path[] rGlob(string pattern) constNote: legacy simple glob/rGlob implementation removed in favor of pathlib-like `glob` and `rglob` implemented later in this struct. For backwards compatibility, provide a camel-cased delegator.bool isReservedDeviceName() constDetect whether this path's final component is a reserved Windows device name.Path home()Returns the user's home directory as a Path.Path cwd()Returns the current working directory as a Path.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() constReturns a lazy input range of `Path` values for each immediate entry in this directory, similar to Python's `Path.iterdir()`.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.string toString() const @safe nothrow @nogc pureString conversion Returns the string representation of this Path.Path[] glob(string pattern) constGlobs for filesystem entries under this directory that match the given pattern.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 3
bool sameFilesystem(string path1, string path2)