std::fs
This module contains file system utilities.
-
To copy a file or directory, use the
cp
command from thestd::cmd
library. -
To rename a file or directory, use the
mv
command from thestd::cmd
library. -
To delete a file or directory, use the
rm
command from thestd::cmd
library. -
To create a new directory, use the
mkdir
command from thestd::cmd
library.
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
true
ifpath
exists. Ifpath
is a symlink, returnstrue
if the target of the link exists. -
is_dir(path: str) -> bool
-
Returns
true
ifpath
exists and is a directory. Ifpath
is a symlink, returnstrue
if the target of the link exists and is a directory. -
is_file(path: str) -> bool
-
Returns
true
ifpath
exists and is a regular file. Ifpath
is a symlink, returnstrue
if the target of the link exists and is a regular file. -
is_symlink(path: str) -> bool
-
Returns
true
ifpath
exists 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
path
is not a directory. -
normpath(path: str) -> str
-
Normalizes the filepath
path
by 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
path
must exist. Equivalent to POSIXrealpath()
.Panics if the path cannot be resolved.