Class in path coil-stdlib

Path

class Path
Import
use path::{Path};
Source: 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#

NameDescription
fromCreates a path from s as written.

Methods#

NameDescription
as_strReturns the path as a string.
joinReturns this path followed by other, with exactly one separator between them.
dirnameReturns the path without its last component.
basenameReturns the last component, ignoring trailing separators ("" for an empty path or /).
extensionReturns the text after the last . of the last component, or "" when there is none.
is_absoluteReturns true when the path starts with a separator or a Windows drive letter (C:).
normalizeRemoves . components, resolves .. against the component before it, and collapses repeated separators.
componentsReturns the components of the normalized path, without separators.
existsReturns whether something exists at this path.
is_fileReturns whether this path is a regular file.
is_dirReturns whether this path is a directory.
metadataReturns the file’s metadata.
list_dirReturns the names of the entries in this directory.
mkdirCreates this directory; its parent must exist.
mkdir_allCreates this directory and any missing parents.
remove_fileDeletes the file at this path.
renameMoves this file or directory to dst.
copy_toCopies this file to dst, replacing it.
read_textReads the whole file as UTF-8 text.
write_textReplaces the file’s contents with text and returns the number of bytes written.
read_bytesReads the whole file.
write_bytesReplaces the file’s contents with buf.
append_textAppends text to the file, creating it if needed.
append_bytesAppends 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");

See also#

↑ ↓ navigate ↵ open