std::fs
This module contains file system utilities.
-
To copy a file or directory, use the
cpcommand from thestd::cmdlibrary. -
To rename a file or directory, use the
mvcommand from thestd::cmdlibrary. -
To delete a file or directory, use the
rmcommand from thestd::cmdlibrary. -
To create a new directory, use the
mkdircommand from thestd::cmdlibrary.
Functions
-
abspath(path: str) -> str -
Returns the normalized, absolute version of the filepath
path. This is equivalent to callingnormpath(join(getcwd(), path)). -
basename(path: str) -> str -
Returns the base name of the filepath
path.Examples:
import std::fs::{basename}; assert basename("") == ""; assert basename("/") == ""; assert basename("/foo.txt") == "foo.txt"; assert basename("foo.txt/") == ""; assert basename("foo.txt") == "foo.txt"; assert basename("foo/bar.txt") == "bar.txt"; assert basename("/foo/bar.txt") == "bar.txt";
-
chdir(path: str) -
Changes the current working directory to
path. -
getcwd() -> str -
Returns the path of the current working directory.
-
dirname(path: str) -> str -
Returns the directory name of the filepath
path.Examples:
import std::fs::{dirname}; assert dirname("") == ""; assert dirname("/") == "/"; assert dirname("/foo.txt") == "/"; assert dirname("foo.txt/") == "foo.txt"; assert dirname("foo.txt") == ""; assert dirname("foo/bar.txt") == "foo"; assert dirname("/foo/bar.txt") == "/foo";
-
exists(path: str) -> bool -
Returns
trueifpathexists. Ifpathis a symlink, returnstrueif the target of the link exists. -
is_dir(path: str) -> bool -
Returns
trueifpathexists and is a directory. Ifpathis a symlink, returnstrueif the target of the link exists and is a directory. -
is_file(path: str) -> bool -
Returns
trueifpathexists and is a regular file. Ifpathis a symlink, returnstrueif the target of the link exists and is a regular file. -
is_symlink(path: str) -> bool -
Returns
trueifpathexists and is a symlink. (This checks if the symlink itself exists, not its target.) -
join(path: str, *paths: str) -> str -
Joins one or more path segments, adding
/separators as required.If a segment is an absolute path — i.e. begins with a
/— all previous segments are ignored and joining continues from the absolute path segment.Examples:
import std::fs::{join}; assert join("") == ""; assert join("foo") == "foo"; assert join("foo", "bar") == "foo/bar"; assert join("/foo/", "bar") == "/foo/bar"; assert join("/foo/", "/bar") == "/bar";
-
listdir(path: str) -> vec[str] -
Returns a vector containing the names of the entries in the directory specified by
path. Skips"."and"..".Panics if
pathis not a directory. -
normpath(path: str) -> str -
Normalizes the filepath
pathby collapsing redundant//and.separators and up-leveling..references.Examples:
import std::fs::{normpath}; assert normpath("foo//bar") == "foo/bar"; assert normpath("foo/./bar") == "foo/bar"; assert normpath("foo/../bar") == "bar";
For paths beginning with exactly two slashes,
//, the leading two slashes are preserved in accordance with POSIX pathname resolution. -
realpath(path: str) -> str -
Returns the canonicalized absolute path, resolving all symbolic links. All the components of
pathmust exist. Equivalent to POSIXrealpath().Panics if the path cannot be resolved.