文件系统
更新时间:2025-11-06
前提条件
- 请按照文档配置环境变量。
- 目前仅支持官方代码沙箱,即template名为 code-interpreter-v1,每个沙箱具备40 GB 免费的临时磁盘存储。
读取文件
1from e2b_code_interpreter import Sandbox
2
3sandbox = Sandbox.create()
4file_content = sandbox.files.read('/path/to/file')
1import { Sandbox } from '@e2b/code-interpreter'
2const sandbox = await Sandbox.create()
3const fileContent = await sandbox.files.read('/path/to/file')
写入单个文件
1from e2b_code_interpreter import Sandbox
2
3sandbox = Sandbox.create()
4
5await sandbox.files.write('/path/to/file', 'file content')
1import { Sandbox } from '@e2b/code-interpreter'
2const sandbox = await Sandbox.create()
3const fileContent = await sandbox.files.read('/path/to/file')
写入多个文件
1from e2b_code_interpreter import Sandbox
2
3sandbox = Sandbox.create()
4
5await sandbox.files.write_files([
6 { "path": "/path/to/a", "data": "file content" },
7 { "path": "another/path/to/b", "data": "file content" }
8])
1import { Sandbox } from '@e2b/code-interpreter'
2const sandbox = await Sandbox.create()
3
4await sandbox.files.write([
5 { path: '/path/to/a', data: 'file content' },
6 { path: '/another/path/to/b', data: 'file content' }
7])
创建文件并获取文件信息
1from e2b_code_interpreter import Sandbox
2
3sandbox = Sandbox.create()
4
5# Create a new file
6sandbox.files.write('test_file', 'Hello, world!')
7
8# Get information about the file
9info = sandbox.files.get_info('test_file')
10
11print(info)
1import { Sandbox } from '@e2b/code-interpreter'
2
3const sandbox = await Sandbox.create()
4
5// Create a new file
6await sandbox.files.write('test_file.txt', 'Hello, world!')
7
8// Get information about the file
9const info = await sandbox.files.getInfo('test_file.txt')
10
11console.log(info)
创建目录并获取目录信息
1from e2b_code_interpreter import Sandbox
2
3sandbox = Sandbox.create()
4
5# Create a new directory
6sandbox.files.make_dir('test_dir')
7
8# Get information about the directory
9info = sandbox.files.get_info('test_dir')
10
11print(info)
1import { Sandbox } from '@e2b/code-interpreter'
2
3const sandbox = await Sandbox.create()
4
5// Create a new directory
6await sandbox.files.makeDir('test_dir')
7
8// Get information about the directory
9const info = await sandbox.files.getInfo('test_dir')
10
11console.log(info)
监视沙箱目录变化
监视沙箱目录变化事件
1from e2b_code_interpreter import Sandbox
2
3sandbox = Sandbox.create()
4dirname = '/home/user'
5
6# watch目标目录变化
7handle = sandbox.files.watch_dir(dirname)
8# Trigger file write event
9sandbox.files.write(f"{dirname}/my-file", "hello")
10
11# 获取目录中文件变化事件
12events = handle.get_new_events()
13for event in events:
14 print(event)
15 if event.type == FilesystemEventType.Write:
16 print(f"wrote to file {event.name}")
1import { Sandbox, FilesystemEventType } from '@e2b/code-interpreter'
2
3const sandbox = await Sandbox.create()
4const dirname = '/home/user'
5
6// watch目标目录变化
7const handle = await sandbox.files.watchDir(dirname, async (event) => {
8 console.log(event)
9 if (event.type === FilesystemEventType.WRITE) {
10 console.log(`wrote to file ${event.name}`)
11 }
12})
13
14// 发起一个文件写入,将导致文件写入事件产生
15await sandbox.files.write(`${dirname}/my-file`, 'hello')
递归监视沙箱文件目录变化
如果需要监视到目录下子文件夹(包括多级嵌套的文件夹)内容的变化,您可以使用 recursive 参数启用 “递归监视”。
1from e2b_code_interpreter import Sandbox
2
3sandbox = Sandbox.create()
4dirname = '/home/user'
5
6# watch目标目录变化
7handle = sandbox.files.watch_dir(dirname, recursive=True)
8# 触发一个写文件事件
9sandbox.files.write(f"{dirname}/my-folder/my-file", "hello")
10
11# 获取目录变化事件并打印
12events = handle.get_new_events()
13for event in events:
14 print(event)
15 if event.type == FilesystemEventType.Write:
16 print(f"wrote to file {event.name}")
1import { Sandbox, FilesystemEventType } from '@e2b/code-interpreter'
2
3const sandbox = await Sandbox.create()
4const dirname = '/home/user'
5
6// 获取目录变化事件并打印
7const handle = await sandbox.files.watchDir(dirname, async (event) => {
8 console.log(event)
9 if (event.type === FilesystemEventType.WRITE) {
10 console.log(`wrote to file ${event.name}`)
11 }
12}, {
13 recursive: true
14})
15
16// 触发一个写文件事件
17await sandbox.files.write(`${dirname}/my-folder/my-file`, 'hello')
上传文件
上传单个文件
1from e2b_code_interpreter import Sandbox
2
3sandbox = Sandbox.create()
4
5# 从本地读取一个文件
6with open("path/to/local/file", "rb") as file:
7# 上传该文件到沙箱
8 sandbox.files.write("/path/in/sandbox", file)
1import fs from 'fs'
2import { Sandbox } from '@e2b/code-interpreter'
3
4const sandbox = await Sandbox.create()
5
6// 从本地读取一个文件
7const content = fs.readFileSync('/local/path')
8// 上传该文件到沙箱
9await sandbox.files.write('/path/in/sandbox', content)
上传目录、多个文件
1import os
2from e2b_code_interpreter import Sandbox
3
4sandbox = Sandbox.create()
5
6def read_directory_files(directory_path):
7 files = []
8
9 # 获取文件夹中各个文件
10 for filename in os.listdir(directory_path):
11 file_path = os.path.join(directory_path, filename)
12
13 # 跳过文件目录
14 if os.path.isfile(file_path):
15 # 以二进制方式读取文件内容
16 with open(file_path, "rb") as file:
17 files.append({
18 'path': file_path,
19 'data': file.read()
20 })
21
22 return files
23
24files = read_directory_files("/local/dir")
25print(files)
26# [
27# {"'path": "/local/dir/file1.txt", "data": "File 1 contents..." },
28# { "path": "/local/dir/file2.txt", "data": "File 2 contents..." },
29# ...
30# ]
31
32sandbox.files.write_files(files)
1const fs = require('fs');
2const path = require('path');
3
4import { Sandbox } from '@e2b/code-interpreter'
5
6const sandbox = await Sandbox.create()
7
8// 获取目录中各个文件内容并写入数组
9const readDirectoryFiles = (directoryPath) => {
10 // Read all files in the local directory
11 const files = fs.readdirSync(directoryPath);
12
13 // 映射文件路径和文件内容
14 const filesArray = files
15 .filter(file => {
16 const fullPath = path.join(directoryPath, file);
17 // 如果是目录则跳过
18 return fs.statSync(fullPath).isFile();
19 })
20 .map(file => {
21 const filePath = path.join(directoryPath, file);
22
23 // Read the content of each file
24 return {
25 path: filePath,
26 data: fs.readFileSync(filePath, 'utf8')
27 };
28 });
29
30 return filesArray;
31};
32
33// 读取文件名和文件内容的map
34const files = readDirectoryContents('/local/dir');
35console.log(files);
36// [
37// { path: '/local/dir/file1.txt', data: 'File 1 contents...' },
38// { path: '/local/dir/file2.txt', data: 'File 2 contents...' },
39// ...
40// ]
41
42await sandbox.files.write_files(files)
从沙箱下载文件
下载单个文件
1from e2b_code_interpreter import Sandbox
2
3sandbox = Sandbox.create()
4
5# 从沙箱读取文件内容
6content = sandbox.files.read('/path/in/sandbox')
7# 将文件写入本地
8with open('/local/path', 'w') as file:
9 file.write(content)
1import fs from 'fs'
2import { Sandbox } from '@e2b/code-interpreter'
3
4const sandbox = await Sandbox.create()
5
6// 从沙箱读取文件内容
7const content = await sandbox.files.read('/path/in/sandbox')
8// 将文件写入本地
9fs.writeFileSync('/local/path', content)
