列出目录用于查看沙箱内的文件结构,适合 Agent 探索工作目录、检查生成产物和定位日志。
列出目录内容
Python 示例:
entries = sandbox.files.list("/tmp")
for entry in entries:
print(entry.name, entry.type, entry.path)TypeScript 示例:
const entries = await sandbox.files.list("/tmp");
for (const entry of entries) {
console.log(entry.name, entry.type, entry.path);
}结合命令执行
Python 示例:
sandbox.commands.run("mkdir -p /tmp/project && touch /tmp/project/app.py")
files = sandbox.files.list("/tmp/project")
print([file.name for file in files])TypeScript 示例:
await sandbox.commands.run("mkdir -p /tmp/project && touch /tmp/project/app.py");
const files = await sandbox.files.list("/tmp/project");
console.log(files.map((file) => file.name));使用建议
目录可能包含较多文件。业务侧需要展示目录树时,建议按需展开,不要在一次请求中递归读取大量路径。
该文章对您有帮助吗?