path
Build and take apart file paths. These functions only look at the text: they never touch the disk, so a path does not need to exist.
Paths use / between parts. To check whether a path exists or is a folder, use fs.exists and fs.is_dir.
Build a path
Section titled “Build a path”import path
print(path.join("src", "main.brass"))print(path.join("src/", "lib/util.brass"))print(path.join("src", "/etc/hosts"))src/main.brasssrc/lib/util.brass/etc/hostsTake a path apart
Section titled “Take a path apart”import path
let p = "docs/guide/intro.tar.gz"print(path.parent(&p))print(path.file_name(&p))print(path.stem(&p))print(path.extension(&p))docs/guideintro.tar.gzintro.targzClean up a path
Section titled “Clean up a path”import path
print(path.normalize("a/./b/../c//d"))print(path.normalize("/../etc"))print(path.normalize("../x/.."))a/c/d/etc..Reference
Section titled “Reference”import pathFunctions
Section titled “Functions”pub fn join(a: &Str, b: &Str) -> StrPaths are text with / between parts; these functions only look at the text, not the disk.
When b starts with /, the result is b alone, as in most languages. Joining text from outside the program (a request path, an archive entry) can therefore point anywhere; use Untrusted.path_inside for that instead.
file_name
Section titled “file_name”pub fn file_name(p: &Str) -> StrThe part after the last /: file_name("a/b.txt") is b.txt.
parent
Section titled “parent”pub fn parent(p: &Str) -> StrEverything before the last part: parent("a/b.txt") is a, parent("b.txt") is ``.
extension
Section titled “extension”pub fn extension(p: &Str) -> StrThe file name’s ending after its last ., without the dot; `` if there is none.
A name that starts with a dot and has no other dot, like .bashrc, has no extension.
import path
print(path.extension(".bashrc").len())print(path.extension("archive.tar.gz"))0gzpub fn stem(p: &Str) -> StrThe file name without its extension: stem("a/b.tar.gz") is b.tar.
is_absolute
Section titled “is_absolute”pub fn is_absolute(p: &Str) -> boolnormalize
Section titled “normalize”pub fn normalize(p: &Str) -> Str. parts are dropped and .. removes the part before it, without touching the disk.
Works on the text only, so a/link/.. becomes a even if link is a symbolic link to another folder. Leading .. parts of a relative path are kept; on an absolute path they are dropped, since /.. is /.