Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
upadte upload docs and improve read-write-docs
0divbbeeb044ccc9
diff --git a/apps/web/src/app/(docs)/docs/filesystem/read-write/page.mdx b/apps/web/src/app/(docs)/docs/filesystem/read-write/page.mdxindex b66e76ecc..a16b35a3a 100644--- a/apps/web/src/app/(docs)/docs/filesystem/read-write/page.mdx+++ b/apps/web/src/app/(docs)/docs/filesystem/read-write/page.mdx@@ -2,7 +2,7 @@## Reading files-You can read files from the sandbox filesystem using the `files.reado()` method.+You can read files from the sandbox filesystem using the `files.read()` method.<CodeGroup>```js@@ -18,19 +18,35 @@ file_content = sandbox.files.read('/path/to/file')```</CodeGroup>-## Writing files+## Writing single files-You can write files to the sandbox filesystem using the `files.write()` method.+You can write signle files to the sandbox filesystem using the `files.write()` method.<CodeGroup>```jsimport { Sandbox } from '@e2b/code-interpreter'const sandbox = await Sandbox.create()-// Write single fileawait sandbox.files.write('/path/to/file', 'file content')+```+```python+from e2b_code_interpreter import Sandbox++sandbox = Sandbox()++await sandbox.files.write('/path/to/file', 'file content')+```+</CodeGroup>++## Writing multiple files++You can also write multiple files to the sandbox filesystem using the `files.write()` method.++<CodeGroup>+```js+import { Sandbox } from '@e2b/code-interpreter'+const sandbox = await Sandbox.create()-// Write multiple filesawait sandbox.files.write([{ path: "/path/to/a", data: "file content" },{ path: "/another/path/to/b", data: "file content" }@@ -41,13 +57,9 @@ from e2b_code_interpreter import Sandboxsandbox = Sandbox()-# Write single file-await sandbox.files.write('/path/to/file', 'file content')--# Write multiple filesawait sandbox.files.write([{ "path": "/path/to/a", "data": "file content" },{ "path": "another/path/to/b", "data": "file content" }])```-</CodeGroup>+</CodeGroup>\ No newline at end of filediff --git a/apps/web/src/app/(docs)/docs/filesystem/upload/page.mdx b/apps/web/src/app/(docs)/docs/filesystem/upload/page.mdxindex 1b16f2668..79064be71 100644--- a/apps/web/src/app/(docs)/docs/filesystem/upload/page.mdx+++ b/apps/web/src/app/(docs)/docs/filesystem/upload/page.mdx@@ -2,6 +2,8 @@You can upload data to the sandbox using the `files.write()` method.+## Upload single file+<CodeGroup>```jsimport fs from 'fs'@@ -25,3 +27,86 @@ with open("path/to/local/file", "rb") as file:sandbox.files.write("/path/in/sandbox", file)```</CodeGroup>++## Upload directory / multiple files++<CodeGroup>+```js+const fs = require('fs');+const path = require('path');++import { Sandbox } from '@e2b/code-interpreter'++const sandbox = await Sandbox.create()++// Read all files in the directory and store their paths and contents in an array+const readDirectoryFiles = (directoryPath) => {+ // Read all files in the local directory+ const files = fs.readdirSync(directoryPath);++ // Map files to objects with path and data+ const filesArray = files+ .filter(file => {+ const fullPath = path.join(directoryPath, file);+ // Skip if it's a directory+ return fs.statSync(fullPath).isFile();+ })+ .map(file => {+ const filePath = path.join(directoryPath, file);++ // Read the content of each file+ return {+ path: filePath,+ data: fs.readFileSync(filePath, 'utf8')+ };+ });++ return filesArray;+};++// Usage example+const files = readDirectoryContents('/local/dir');+console.log(files);+// [+// { path: '/local/dir/file1.txt', data: 'File 1 contents...' },+// { path: '/local/dir/file2.txt', data: 'File 2 contents...' },+// ...+// ]++await sandbox.files.write(files)+```+```python+import os+from e2b_code_interpreter import Sandbox++sandbox = Sandbox()++def read_directory_files(directory_path):+ files = []++ # Iterate through all files in the directory+ for filename in os.listdir(directory_path):+ file_path = os.path.join(directory_path, filename)++ # Skip if it's a directory+ if os.path.isfile(file_path):+ # Read file contents in binary mode+ with open(file_path, "rb") as file:+ files.append({+ 'path': file_path,+ 'data': file.read()+ })++ return files++files = read_directory_files('/local/dir');+print(files);+// [+// { 'path': '/local/dir/file1.txt', 'data': 'File 1 contents...' },+// { 'path': '/local/dir/file2.txt', 'data': 'File 2 contents...' },+// ...+// ]++sandbox.files.write(files)+```+</CodeGroup>\ No newline at end of file