API: wingman.file
文件 IO 模块,基于 std::filesystem 提供常用文件与目录操作,二进制安全、路径支持非 ASCII 字符。
模块概述
file 模块提供以下能力:
- 读写文件 - 整文件读入 / 截断写 / 追加写(二进制安全)
- 路径查询 - 存在性、文件/目录判断、大小
- 文件操作 - 移动、复制、删除、递归删除
- 目录操作 - 递归创建、列目录
注意:所有系统调用异常都转为 nil/false 返回值,不向脚本引擎抛出;write 不自动创建父目录,需先 mkdir。
读文件
read(path) / read(path)
说明:整文件读入字符串(二进制安全)。文件不存在或读取失败返回 nil。
函数签名:
python
read(path: str) -> str | Nonelua
read(path: string) -> string | nil参数:
path- 文件路径
返回:
str/string- 文件内容;失败返回None/nil
python
from wingman import file
content = file.read("config.json")
if content is not None:
print(content)写文件
write(path, content) / write(path, content)
说明:截断写(覆盖已有内容)。不自动创建父目录。
函数签名:
python
write(path: str, content: str) -> boollua
write(path: string, content: string) -> boolean参数:
path- 目标文件路径content- 写入内容(二进制安全)
返回:
bool/boolean- 是否成功
python
from wingman import file
ok = file.write("out/result.txt", "hello\n")append(path, content) / append(path, content)
说明:追加写到文件末尾(文件不存在则创建)。
函数签名:
python
append(path: str, content: str) -> boollua
append(path: string, content: string) -> boolean参数:
path- 目标文件路径content- 追加内容
返回:
bool/boolean- 是否成功
python
from wingman import file
file.append("log.txt", "line\n")路径查询
exists(path) / exists(path)
说明:路径是否存在(文件或目录)。
函数签名:
python
exists(path: str) -> boollua
exists(path: string) -> booleanpython
from wingman import file
if file.exists("data/kv.db"):
print("exists")isFile(path) / isFile(path)
说明:路径是否为常规文件。
函数签名:
python
isFile(path: str) -> boollua
isFile(path: string) -> booleanisDir(path) / isDir(path)
说明:路径是否为目录。
函数签名:
python
isDir(path: str) -> boollua
isDir(path: string) -> booleansize(path) / size(path)
说明:文件大小(字节)。路径不存在或不可获取时返回 nil;0 是合法大小。
函数签名:
python
size(path: str) -> int | Nonelua
size(path: string) -> number | nilpython
from wingman import file
sz = file.size("out/result.txt")
if sz is not None:
print(f"{sz} bytes")文件操作
move(src, dst) / move(src, dst)
说明:移动/重命名。rename 优先,跨文件系统时回退为 copy+remove。
函数签名:
python
move(src: str, dst: str) -> boollua
move(src: string, dst: string) -> boolean参数:
src- 源路径dst- 目标路径
返回:
bool/boolean- 是否成功
copy(src, dst) / copy(src, dst)
说明:复制文件或目录(目录递归复制;目标已存在则覆盖)。
函数签名:
python
copy(src: str, dst: str) -> boollua
copy(src: string, dst: string) -> booleanremove(path) / remove(path)
说明:删除单个文件或空目录。递归删除用 removeAll。
函数签名:
python
remove(path: str) -> boollua
remove(path: string) -> booleanremoveAll(path) / removeAll(path)
说明:递归删除文件或目录树。路径不存在时也返回 true(幂等)。
函数签名:
python
removeAll(path: str) -> boollua
removeAll(path: string) -> booleanpython
from wingman import file
file.remove("out/result.txt")
file.removeAll("out/tmp") # 目录树递归删目录操作
mkdir(path) / mkdir(path)
说明:递归创建目录(含不存在的父级);目录已存在时返回 true。
函数签名:
python
mkdir(path: str) -> boollua
mkdir(path: string) -> booleanlistDir(path) / listDir(path)
说明:列出目录下的条目名(不含 . ..,按名称排序)。仅条目名,不含路径。非目录或读取失败返回 nil。
函数签名:
python
listDir(path: str) -> list[str] | Nonelua
listDir(path: string) -> string[] | nilpython
from wingman import file
names = file.listDir("out")
if names is not None:
for name in names:
print(name)完整示例
Python
python
from wingman import file
# 建目录并写文件
file.mkdir("out")
file.write("out/result.txt", "hello")
# 读回并追加
content = file.read("out/result.txt")
file.append("out/result.txt", "world")
# 目录遍历
for name in file.listDir("out") or []:
print(name, file.size(f"out/{name}"))
# 清理
file.removeAll("out")Lua
lua
local wingman = require("wingman")
-- 建目录并写文件
wingman.file.mkdir("out")
wingman.file.write("out/result.txt", "hello")
-- 读回并追加
local content = wingman.file.read("out/result.txt")
wingman.file.append("out/result.txt", "world")
-- 目录遍历
local names = wingman.file.listDir("out")
if names ~= nil then
for _, name in ipairs(names) do
print(name)
end
end
-- 清理
wingman.file.removeAll("out")可用接口
| Python 函数 | Lua 函数 | 说明 | 参数 |
|---|---|---|---|
read(path) | read(path) | 整文件读入(二进制安全) | path: 文件路径 |
write(path, content) | write(path, content) | 截断写 | path: 路径 content: 内容 |
append(path, content) | append(path, content) | 追加写 | path: 路径 content: 内容 |
exists(path) | exists(path) | 是否存在 | path: 路径 |
isFile(path) | isFile(path) | 是否为常规文件 | path: 路径 |
isDir(path) | isDir(path) | 是否为目录 | path: 路径 |
size(path) | size(path) | 文件大小(字节) | path: 路径 |
move(src, dst) | move(src, dst) | 移动/重命名 | src: 源路径 dst: 目标路径 |
copy(src, dst) | copy(src, dst) | 复制(目录递归) | src: 源路径 dst: 目标路径 |
remove(path) | remove(path) | 删除文件或空目录 | path: 路径 |
removeAll(path) | removeAll(path) | 递归删除(幂等) | path: 路径 |
mkdir(path) | mkdir(path) | 递归创建目录 | path: 路径 |
listDir(path) | listDir(path) | 列目录条目名(排序) | path: 目录路径 |