Path
class Path Import
Source: use path::{Path}; src/path.hy A file-system path, with helpers to take it apart and to use it.
Path is plain text: building one never touches the disk. Both / and \\
separate components; join and normalize write /. The file-system methods
(exists, read_text, mkdir, …) call io::fs and io::file, so they need
the same --allow-read / --allow-write grants.
Associated functions#
| Name | Description |
|---|---|
from | Creates a path from s as written. |
Methods#
| Name | Description |
|---|---|
as_str | Returns the path as a string. |
join | Returns this path followed by other, with exactly one separator between them. |
dirname | Returns the path without its last component. |
basename | Returns the last component, ignoring trailing separators ("" for an empty path or /). |
extension | Returns the text after the last . of the last component, or "" when there is none. |
is_absolute | Returns true when the path starts with a separator or a Windows drive letter (C:). |
normalize | Removes . components, resolves .. against the component before it, and collapses repeated separators. |
components | Returns the components of the normalized path, without separators. |
exists | Returns whether something exists at this path. |
is_file | Returns whether this path is a regular file. |
is_dir | Returns whether this path is a directory. |
metadata | Returns the file’s metadata. |
list_dir | Returns the names of the entries in this directory. |
mkdir | Creates this directory; its parent must exist. |
mkdir_all | Creates this directory and any missing parents. |
remove_file | Deletes the file at this path. |
rename | Moves this file or directory to dst. |
copy_to | Copies this file to dst, replacing it. |
read_text | Reads the whole file as UTF-8 text. |
write_text | Replaces the file’s contents with text and returns the number of bytes written. |
read_bytes | Reads the whole file. |
write_bytes | Replaces the file’s contents with buf. |
append_text | Appends text to the file, creating it if needed. |
append_bytes | Appends buf to the file, creating it if needed. |
Examples#
use path::{Path};
use tests::assert;
let p = Path::from("/srv/app/../data/report.csv").normalize() ?? Path::from("");
assert!(p.as_str() == "/srv/data/report.csv");
assert!((p.basename() ?? "") == "report.csv");
assert!((p.extension() ?? "") == "csv");
assert!((p.dirname() ?? p).as_str() == "/srv/data");