ddn.os.path¶
Utilities for file and directory path manipulation, inspired by Python's pathlib.
Module Declaration¶
Structs¶
Path¶
Cross-platform Path object inspired by Python's pathlib. Supports Windows, Linux, and POSIX environments.
Constructor¶
Constructs a Path from a string.
| Parameter | Description |
|---|---|
path |
The path string |
Example:
Methods¶
str¶
Returns the underlying path string.
Returns: The path as a string.
Attributes: @safe nothrow @nogc pure
name¶
Returns the final component of the path (file name with extension).
Returns: The file name.
Example:
stem¶
Returns the file name without the extension.
Returns: The stem of the file name.
Example:
suffix¶
Returns the file extension (including the dot).
Returns: The file extension.
Example:
parent¶
Returns the parent directory of this path.
Returns: A Path representing the parent directory.
Example:
auto p = Path("/home/user/documents/report.txt");
assert(p.parent().str() == "/home/user/documents");
parents¶
Returns a range of all parent directories.
Returns: A range of Path objects representing all ancestors.
Example:
auto p = Path("/home/user/documents/report.txt");
foreach (par; p.parents()) {
writeln(par.str());
}
// Output:
// /home/user/documents
// /home/user
// /home
join¶
Joins this path with another path component.
| Parameter | Description |
|---|---|
other |
The path component to join |
Returns: A new Path with the joined components.
opBinary!"/"¶
Joins paths using the / operator.
Returns: A new Path with the joined components.
Example:
absolute¶
Returns the absolute path.
Returns: A Path representing the absolute path.
resolve¶
Resolves the path to an absolute path with normalization (removes . and .. components).
Returns: A normalized absolute Path.
normalize¶
Normalizes the path (removes redundant separators and . / .. components).
Returns: A normalized Path.
Example:
relativeTo¶
Returns this path relative to the given base path.
| Parameter | Description |
|---|---|
base |
The base path |
Returns: A relative Path.
exists¶
Checks if the path exists in the filesystem.
Returns: true if the path exists.
isFile¶
Checks if the path is a regular file.
Returns: true if the path is a file.
isDir¶
Checks if the path is a directory.
Returns: true if the path is a directory.
isSymlink¶
Checks if the path is a symbolic link.
Returns: true if the path is a symlink.
isAbsolute¶
Checks if the path is absolute.
Returns: true if the path is absolute.
isBlockDevice¶
Returns true if the path is a block device.
On POSIX, this checks if the file type is a block device. On Windows, always returns false.
Returns: true if the path is a block device.
isCharDevice¶
Returns true if the path is a character device.
Returns: true if the path is a character device.
isFIFO¶
Returns true if the path is a FIFO (named pipe).
Returns: true if the path is a FIFO.
isSocket¶
Returns true if the path is a socket.
Returns: true if the path is a socket.
readText¶
Reads the entire contents of the file as text.
Returns: The file contents as a string.
writeText¶
Writes text content to the file.
| Parameter | Description |
|---|---|
content |
The text to write |
touch¶
Creates the file if it doesn't exist, or updates its modification timestamp.
remove¶
Removes the file or directory (recursive for directories).
mkdir¶
Creates the directory, including any necessary parent directories.
rmdir¶
Removes an empty directory.
glob¶
Finds files matching a glob pattern in this directory.
| Parameter | Description |
|---|---|
pattern |
The glob pattern (e.g., "*.txt", "**/*.d") |
Returns: A range of matching Path objects.
Example:
auto dir = Path("/var/log");
auto txtFiles = dir.glob("*.txt");
auto nestedTxt = dir.glob("**/*.txt");
rglob¶
Recursively finds files matching a glob pattern.
| Parameter | Description |
|---|---|
pattern |
The glob pattern |
Returns: A range of matching Path objects.
iterdir¶
Iterates over the contents of this directory.
Returns: A range of Path objects for directory entries.
Example:
walk¶
Recursively walks the directory tree.
| Parameter | Description |
|---|---|
topDown |
If true, yield directories before their contents |
followSymlinks |
If true, follow symbolic links |
Returns: A range of walk entries containing root, dirs, and files.
Example:
foreach (entry; Path("/project").walk()) {
writeln("Directory: ", entry.root.str());
foreach (d; entry.dirs) {
writeln(" Subdir: ", d.str());
}
foreach (f; entry.files) {
writeln(" File: ", f.str());
}
}
symlinkTo¶
Creates a symbolic link at this path pointing to the target.
| Parameter | Description |
|---|---|
target |
The target path |
hardlinkTo¶
Creates a hard link at this path pointing to the target.
| Parameter | Description |
|---|---|
target |
The target path |
readlink¶
Reads the target of a symbolic link.
Returns: The symlink target as a string.
samefile¶
Checks if two paths refer to the same file.
| Parameter | Description |
|---|---|
other |
The other path to compare |
Returns: true if both paths refer to the same file.
owner¶
Returns the owner username of the file (POSIX only).
Returns: The owner's username.
group¶
Returns the group name of the file (POSIX only).
Returns: The group name.
toUri¶
Converts the path to a file URI.
Returns: A file:// URI string.
withName¶
Returns a new path with the file name replaced.
| Parameter | Description |
|---|---|
newName |
The new file name |
Returns: A new Path with the replaced name.
Example:
withSuffix¶
Returns a new path with the file extension replaced.
| Parameter | Description |
|---|---|
newSuffix |
The new extension (including dot) |
Returns: A new Path with the replaced extension.
Example:
expandTilde¶
Expands ~ to the user's home directory.
Returns: A Path with tilde expanded.
expandVars¶
Expands environment variables in the path.
Returns: A Path with variables expanded.
Example:
Static Methods¶
home¶
Returns the current user's home directory.
Returns: A Path representing the home directory.
cwd¶
Returns the current working directory.
Returns: A Path representing the current directory.
See Also¶
- Python's pathlib — Inspiration for this module