文件系统 | 大装置帮助中心
跳到主要内容

文件系统

支持沙箱内单文件/多文件读写、创建目录、获取文件或目录信息、监听目录变更;

支持函数

函数名称说明
sandbox.files.write(path, content)/sandbox.files.read(path)单文件写、读
sandbox.files.write_files([{"path": p, "data": d}, ...])批量文件写入
sandbox.files.make_dir(path)创建目录
sandbox.files.get_info(path)获取 EntryInfo(name, type, path, size, mode, permissions, owner, group, modified_time, symlink_target)
sandbox.files.list(path)列出沙箱内指定目录下的所有条目(文件、子目录);path为必填;opts为可选参数,包含请求超时等连接/列举配置;返回EntryInfo
sandbox.files.exists(path)检查沙箱内指定路径的文件/目录是否存在;path必填参数;opts为可选参数,包含请求超时等连接配置;返回布尔值:true=存在,flase=不存在
sandbox.files.remove(path)删除文件或目录;path为必填参数,opts为可选参数;包含请求超时等链接配置;无返回值,仅等待删除操作完成
sandbox.files.rename(old_path, new_path)重命名文件或目录;opts为可选参数,包含请求超时等链接配置;返回重命名后的 EntryInfo

使用示例

# 单文件读写
sandbox.files.write("/tmp/test_file.txt", "Hello, e2b SDK!")
content = sandbox.files.read("/tmp/test_file.txt")

# 多文件写入
sandbox.files.write_files([
{"path": "/tmp/multi1.txt", "data": "First content"},
{"path": "/tmp/multi2.txt", "data": "Second content"},
])

# 目录与信息
sandbox.files.make_dir("test_dir")
info = sandbox.files.get_info("test_dir") # info.name, info.type (FileType.DIR) 等

# 目录监听
handle = sandbox.files.watch_dir("/tmp", recursive=True)
sandbox.files.write("/tmp/my-folder/my-file", "hello")
events = handle.get_new_events()
for event in events:
if event.type == FilesystemEventType.WRITE:
print(f"wrote to file {event.name}")