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

文件系统

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

支持函数

  • 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.watch_dir(dirname, recursive=...):监听目录变更,返回 handle;
  • handle.get_new_events() 获取事件,事件类型如 FilesystemEventType.WRITE
  • sandbox.upload_url(path=..., use_signature_expiration=...):预签名 URL 上传

使用示例

# 单文件读写
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}")