std.fs
pub const APPEND: int
Sets the option for the append mode.
This option means that writes will append to a file instead of overwriting previous contents.
Note that setting both WRITE|APPEND
has the same effect as setting only APPEND
.
See also: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.append
pub const CREATE: int
Sets the option to create a new file, or open it if it already exists.
In order for the file to be created, WRITE
or APPEND
must be used.
See also: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create
pub const CREATE_NEW: int
Sets the option to create a new file, failing if it already exists.
If CREATE_NEW
is set, the CREATE
and TRUNCATE
flags are ignored.
Must be used with WRITE
or APPEND
to create a new file.
See also: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create_new
pub const MAIN_SEPARATOR: string
The primary separator of path components for the current platform.
For example, /
on Unix and \
on Windows.
pub const READ: int
Sets the option for read access.
This option, means will indicate that the file should be read
-able if opened.
See also: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.read
Copies the contents of one file to another. This function will also copy the permission bits of the original file to the destination file.
Copies the contents of one file to another. This function will also copy the permission bits of the original file to the destination file.
Copies the contents of one file to another. This function will also copy the permission bits of the original file to the destination file.
This function will overwrite the contents of to.
See also: https://doc.rust-lang.org/std/fs/fn.copy.html
use std.fs;
fn main() throws {
try fs.copy("foo.txt", "bar.txt");
// Now, `foo.txt` content is copied to `bar.txt`.
}
copy_dir
pub fn copy_dir(from: string, to: string, force: bool = false, recursive: bool = false) throws IoError
Copies a dir to another location.
copy_dir
pub fn copy_dir(from: string, to: string, force: bool = false, recursive: bool = false) throws IoError
Copies a dir to another location.
Copies a dir to another location.
Options
force
: Overwrite the destination directory. Othewise an error if the destination directory already exists. default istrue
.recursive
: Recursively copy a directory with a new name or place it inside the destination (same behaviors ascp -R`` on Unix). default is
false`.
Errors
- This
from
path is not a directory. - This
from
directory does not exist. - Invalid folder name for
from
orto
. - The current process does not have the permission to access
from
or writeto
.
Example
Copy a directory to another location as subdirectory.
use std.fs;
fn main() throws {
try fs.copy_dir("foo", "bar");
}
If there have "foo/test.txt", now foo
directory is copied to bar/foo
, and bar
folder have: bar/foo/test.txt
.
bar
foo
test.txt
Copy a directory to another location as new directory.
use std.fs;
fn main() throws {
try fs.copy_dir("foo", "bar", recursive: true);
}
If there have "foo1/test.txt", now foo1
directory is copied to bar1
, and bar1
folder have: bar1/test.txt
.
bar1
test.txt
create_dir
pub fn create_dir(path: string) throws IoError
Creates a new, empty directory at the provided path
create_dir
pub fn create_dir(path: string) throws IoError
Creates a new, empty directory at the provided path
Creates a new, empty directory at the provided path
If a parent of the given path doesn't exist, this function will return an error.
To create a directory and all its missing parents at the same time, use the create_dir_all
function.
use std.fs;
fn main() throws {
try fs.create_dir("foo/bar");
}
create_dir_all
pub fn create_dir_all(path: string) throws IoError
Recursively create a directory and all of its parent components if they are missing.
create_dir_all
pub fn create_dir_all(path: string) throws IoError
Recursively create a directory and all of its parent components if they are missing.
Recursively create a directory and all of its parent components if they are missing.
Same as mkdir -p
in Unix.
use std.fs;
fn main() throws {
try fs.create_dir_all("foo/bar/dar");
// Now, will create `foo/bar/dar` directory.
}
Check if a path exists.
Check if a path exists.
Check if a path exists.
use std.fs;
fn main() throws {
fs.exists("foo.txt"); // => true
}
Glob returns all matched file paths by given pattern, return a []string
.
Glob returns all matched file paths by given pattern, return a []string
.
Glob returns all matched file paths by given pattern, return a []string
.
use std.fs;
fn main() throws {
let filepaths: [string] = [];
for (let p in try fs.glob("/home/username/**/*.nv")) {
filepaths.push(p);
}
filepaths;
// ["/home/username/test.nv", "/home/username/foo/bar/test.nv"]
}
Creates a new hard link on the filesystem.
Creates a new hard link on the filesystem.
Creates a new hard link on the filesystem.
The link
path will be a link pointing to the original
path.
Note that systems often require these two paths to both be located on the same filesystem.
If original
names a symbolic link, it is platform-specific whether the symbolic link is followed.
On platforms where it’s possible to not follow it, it is not followed, and the created hard link points to the symbolic link itself.
See also: https://doc.rust-lang.org/std/fs/fn.hard_link.html
use std.fs;
fn main() throws {
try fs.link("foo.txt", "bar.txt");
// Now, `bar.txt` is a hard link to `foo.txt`.
}
Return the metadata of the file.
Return the metadata of the file.
Return the metadata of the file.
use std.fs;
fn main() throws {
let meta = try fs.metadata("foo.txt");
meta.accessed(); // DateTime? of the last access time
meta.modified(); // DateTime? of the last modified time
}
Read the entire contents of a file into a Bytes
.
Read the entire contents of a file into a Bytes
.
Read the entire contents of a file into a Bytes
.
use std.fs;
fn main() throws {
let data = try fs.read("foo.txt");
assert_eq data.to_string(), "Hello, world!";
}
Returns entries in a directory.
Returns entries in a directory.
Returns entries in a directory.
read_to_string
pub fn read_to_string(path: string): string throws IoError
Read the entire contents of a file into a string.
read_to_string
pub fn read_to_string(path: string): string throws IoError
Read the entire contents of a file into a string.
Read the entire contents of a file into a string.
use std.fs;
fn main() throws {
let s = try fs.read_to_string("foo.txt");
assert_eq s, "Hello, world!";
}
Reads a symbolic link, returning the file path that the `link`` points to.
Reads a symbolic link, returning the file path that the `link`` points to.
Reads a symbolic link, returning the file path that the `link`` points to.
Errors
This function will return an error in the following situations, but is not limited to just these cases:
path
is not a symbolic link.path
does not exist.
use std.fs;
fn main() throws {
try fs.symlink("path/of/foo.txt", "path/of/bar.txt");
let p = try fs.readlink("path/of/bar.txt");
assert_eq p, "path/of/foo.txt";
}
remove_dir
pub fn remove_dir(path: string) throws IoError
Remove an empty directory.
remove_dir
pub fn remove_dir(path: string) throws IoError
Remove an empty directory.
Remove an empty directory.
If the directory is not empty, it will fail.
use std.fs;
fn main() throws {
try fs.remove_dir("foo/bar");
}
remove_dir_all
pub fn remove_dir_all(path: string) throws IoError
Removes a directory at this path, after removing all its contents. Use carefully!
remove_dir_all
pub fn remove_dir_all(path: string) throws IoError
Removes a directory at this path, after removing all its contents. Use carefully!
Removes a directory at this path, after removing all its contents. Use carefully!
use std.fs;
fn main() throws {
try fs.remove_dir_all("foo/bar");
}
remove_file
pub fn remove_file(path: string) throws IoError
Remove a file.
remove_file
pub fn remove_file(path: string) throws IoError
Remove a file.
Remove a file.
use std.fs;
fn main() throws {
try fs.remove_file("foo.txt");
}
Rename a file or directory to a new name, replacing the original file if to already exists.
Rename a file or directory to a new name, replacing the original file if to already exists.
Rename a file or directory to a new name, replacing the original file if to already exists.
This will not work if the new name is on a different mount point.
use std.fs;
use std.path;
fn main() throws {
try fs.rename("foo.txt", "bar.txt");
// Now, `foo.txt` is renamed to `bar.txt`.
assert !path.exists("foo.txt");
}
Creates a new symbolic link on the filesystem.
Creates a new symbolic link on the filesystem.
Creates a new symbolic link on the filesystem.
The link
path will be a symbolic link
pointing to the original path.
use std.fs;
fn main() throws {
try fs.symlink("foo.txt", "bar.txt");
// Now, `bar.txt` is a symbolic link to `foo.txt`.
}
See also:
- https://doc.rust-lang.org/std/fs/fn.soft_link.html
- Unix: https://doc.rust-lang.org/std/os/unix/fs/fn.symlink.html
- Windows: https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_file.html
Remove a file.
Remove a file.
Remove a file.
use std.fs;
fn main() throws {
try fs.remove_file("foo.txt");
}
Write bytes to a file.
Write bytes to a file.
Write bytes to a file.
use std.io.Bytes;
use std.fs;
fn main() throws {
let bytes = Bytes.from_array([104, 101, 108, 108, 111]);
try fs.write("foo.txt", bytes);
}
write_string
pub fn write_string(path: string, s: string) throws IoError
Write string to a file.
write_string
pub fn write_string(path: string, s: string) throws IoError
Write string to a file.
Write string to a file.
use std.fs;
fn main() throws {
try fs.write_string("foo.txt", "Hello, world!");
}