std.fs
路径级文件系统操作:整文件读写、目录管理与遍历、文件元数据、临时文件与路径运算。
需要按句柄增量读写(open/read/seek)时用 std.os;控制台输入输出用 std.io。
use std.fs平台可用性
本模块在 wasm32 目标上不导出(无文件系统语义)。mkdtemp / tmpfile / temp_dir 依赖安全临时文件创建,仅在原生目标可用。
函数一览
| 函数 | 签名 |
|---|---|
read_file | (path: &String) -> String |
write_file | (path: &String, content: &String) -> Bool |
append_file | (path: &String, content: &String) -> Bool |
exists | (path: &String) -> Bool |
is_file | (path: &String) -> Bool |
is_dir | (path: &String) -> Bool |
mkdir | (path: &String) -> Bool |
mkdir_all | (path: &String) -> Bool |
rmdir | (path: &String) -> Bool |
remove | (path: &String) -> Bool |
copy | (src: &String, dst: &String) -> Bool |
rename | (src: &String, dst: &String) -> Bool |
read_dir | (path: &String) -> Vec(String) |
walk | (path: &String) -> Vec(String) |
stat | (path: &String) -> Dict(String, Any) |
temp_dir | () -> String |
mkdtemp | (prefix: &String) -> String |
tmpfile | (prefix: &String) -> String |
path_join | (base: &String, rel: &String) -> String |
path_basename | (path: &String) -> String |
path_dirname | (path: &String) -> String |
path_extension | (path: &String) -> String |
函数
read_file
read_file: (path: &String) -> String一次性读取整个文件内容为字符串。
path—— 文件路径(只读借用)
返回:文件全部内容。错误:文件不存在或无权限时抛出 E6007。不返回空串。
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_read_file.txt"
fs.write_file(p, "hello")
content = fs.read_file(p)
assert(content == "hello")
fs.remove(p)
}write_file
write_file: (path: &String, content: &String) -> Bool把 content 写入 path,覆盖原有内容;文件不存在时创建。
path—— 文件路径(只读借用)content—— 要写入的内容(只读借用)
返回:写入成功返回 true。错误:目录不存在或无权限时抛出 E6007(不返回 false)。
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_write_file.txt"
ok = fs.write_file(p, "hello")
assert(ok)
assert(fs.exists(p))
fs.remove(p)
}append_file
append_file: (path: &String, content: &String) -> Bool把 content 追加到 path 末尾;文件不存在时创建。
返回:写入成功返回 true。错误:无权限时抛出 E6007。
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_append_file.txt"
fs.write_file(p, "hello")
fs.append_file(p, " world")
assert(fs.read_file(p) == "hello world")
fs.remove(p)
}exists
exists: (path: &String) -> Bool路径是否存在(文件或目录均可)。
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_exists.txt"
assert(!fs.exists(p), "absent before write")
fs.write_file(p, "x")
assert(fs.exists(p), "present after write")
fs.remove(p)
}is_file
is_file: (path: &String) -> Bool路径是否指向常规文件。路径不存在时返回 false(不报错)。
is_dir
is_dir: (path: &String) -> Bool路径是否指向目录。路径不存在时返回 false(不报错)。
mkdir
mkdir: (path: &String) -> Bool创建单层目录,父目录必须已存在。需要递归创建时用 mkdir_all。
返回:成功返回 true。错误:父目录缺失或目录已存在时抛出 E6007。
mkdir_all
mkdir_all: (path: &String) -> Bool递归创建目录,缺失的父目录一并创建;目录已存在时视为成功。
返回:成功返回 true。错误:无权限时抛出 E6007。
use std.assert
use std.fs
main: () -> Void = {
d = "__yx_doc_fs_mkdir_all/a/b"
ok = fs.mkdir_all(d)
assert(ok)
assert(fs.is_dir(d))
fs.rmdir("__yx_doc_fs_mkdir_all/a/b")
fs.rmdir("__yx_doc_fs_mkdir_all/a")
fs.rmdir("__yx_doc_fs_mkdir_all")
}rmdir
rmdir: (path: &String) -> Bool删除空目录。非空目录删除失败并抛出 E6007。
remove
remove: (path: &String) -> Bool删除文件。删除目录用 rmdir(仅限空目录)。
返回:成功返回 true。错误:文件不存在时抛出 E6007。
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_remove.txt"
fs.write_file(p, "x")
assert(fs.remove(p), "remove ok")
assert(!fs.exists(p), "gone after remove")
}copy
copy: (src: &String, dst: &String) -> Bool复制文件内容与权限位到目标路径;目标已存在时被覆盖(与 Rust fs::copy 一致, 不复制元数据时间戳)。
返回:成功返回 true。错误:源不存在时抛出 E6007。
rename
rename: (src: &String, dst: &String) -> Bool重命名/移动文件或目录。跨设备移动时抛出 E6007(先 copy 后 remove 替代)。
返回:成功返回 true。
use std.assert
use std.fs
main: () -> Void = {
a = "__yx_doc_fs_rename_a.txt"
b = "__yx_doc_fs_rename_b.txt"
fs.write_file(a, "data")
assert(fs.rename(a, b), "rename ok")
assert(fs.exists(b), "target exists")
assert(!fs.exists(a), "source gone")
fs.remove(b)
}read_dir
read_dir: (path: &String) -> Vec(String)列出目录下的条目名称(不含路径前缀),返回 List(String),按名称排序。
与原
std.os.read_dir(返回"\n"连接的字符串)不同,本函数返回类型正确的 List——这是std.os文件操作迁入std.fs时的语义升级。
use std.assert
use std.fs
use std.list
main: () -> Void = {
d = "__yx_doc_fs_readdir"
fs.mkdir_all(d)
fs.write_file(d + "/b.txt", "b")
fs.write_file(d + "/a.txt", "a")
names = fs.read_dir(d)
assert(list.len(names) == 2, "two entries")
assert(list.get(names, 0) == "a.txt", "sorted first")
assert(list.get(names, 1) == "b.txt", "sorted second")
fs.remove(d + "/a.txt")
fs.remove(d + "/b.txt")
fs.rmdir(d)
}walk
walk: (path: &String) -> Vec(String)递归遍历目录树,返回全部条目的完整路径 List(String);每层按名称排序, 目录先于其内容出现(深度优先)。符号链接本身作为条目列出但不深入。
错误:path 不是目录时抛出 E6007。
use std.assert
use std.fs
use std.list
main: () -> Void = {
d = "__yx_doc_fs_walk"
fs.mkdir_all(d + "/sub")
fs.write_file(d + "/a.txt", "a")
fs.write_file(d + "/sub/b.txt", "b")
paths = fs.walk(d)
// a.txt、sub 目录、sub/b.txt 三个条目
assert(list.len(paths) == 3, "2 files + 1 dir")
fs.remove(d + "/a.txt")
fs.remove(d + "/sub/b.txt")
fs.rmdir(d + "/sub")
fs.rmdir(d)
}stat
stat: (path: &String) -> Dict(String, Any)读取文件/目录元数据,返回字典:
| 键 | 类型 | 含义 |
|---|---|---|
size | Int | 字节数 |
is_dir | Bool | 是否目录 |
is_file | Bool | 是否常规文件 |
readonly | Bool | 是否只读权限 |
mtime | Int | 修改时间(Unix 秒) |
错误:路径不存在时抛出 E6007。
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_stat.txt"
fs.write_file(p, "12345")
st = fs.stat(p)
assert(st["size"] == 5, "byte size")
assert(st["is_file"], "is a file")
fs.remove(p)
}temp_dir
temp_dir: () -> String系统临时目录路径(Windows 为 %TEMP%,Unix 为 $TMPDIR 或 /tmp)。
mkdtemp
mkdtemp: (prefix: &String) -> String在系统临时目录内创建唯一临时目录(名字以 prefix 开头),返回其完整路径。
目录不会被自动回收:脚本用完自行
rmdir。这个语义是刻意的—— 显式生命周期比隐式 drop 钩子可预测。
返回:新目录路径。错误:创建失败时抛出 E6007。
use std.assert
use std.fs
main: () -> Void = {
d = fs.mkdtemp("yx_doc_")
assert(fs.is_dir(d), "temp dir created")
fs.rmdir(d)
}tmpfile
tmpfile: (prefix: &String) -> String在系统临时目录内创建唯一空临时文件(名字以 prefix 开头),返回其完整路径。 同样不会自动回收,用完自行 remove。
use std.assert
use std.fs
main: () -> Void = {
p = fs.tmpfile("yx_doc_")
assert(fs.is_file(p), "temp file created")
fs.write_file(p, "scratch")
assert(fs.read_file(p) == "scratch")
fs.remove(p)
}path_join
path_join: (base: &String, rel: &String) -> String拼接路径分量。rel 为绝对路径时直接替换 base(与 Rust/Python 一致)。 分隔符按平台(Windows 为 \)。
use std.assert
use std.fs
use std.string
main: () -> Void = {
joined = fs.path_join("dir", "file.txt")
assert(string.ends_with(joined, "file.txt"), "suffix kept")
assert(string.starts_with(joined, "dir"), "base kept")
}path_basename
path_basename: (path: &String) -> String路径的最终分量;无最终分量(如 /、..)返回空串。
path_dirname
path_dirname: (path: &String) -> String路径的目录部分;无父目录(如 a.txt)返回空串。
use std.assert
use std.fs
main: () -> Void = {
assert(fs.path_basename("dir/file.txt") == "file.txt")
assert(fs.path_dirname("dir/file.txt") == "dir")
assert(fs.path_extension("a.tar.gz") == "gz")
assert(fs.path_extension("noext") == "")
}path_extension
path_extension: (path: &String) -> String扩展名(不含点,取最后一个);无扩展名返回空串。
