> ## Documentation Index
> Fetch the complete documentation index at: https://ppio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 文件读写

export const SandboxConfigHint = () => {
  if (typeof document === "undefined") {
    return null;
  } else {
    return <Note>在运行本文档中的示例代码前，请确保您已正确配置环境变量，详情请参考 <a href="/sandbox/get-start#配置环境变量">配置环境变量</a>。</Note>;
  }
};

<SandboxConfigHint />

## 读取文件

您可以使用 `files.read()` 方法从沙箱文件系统中读取文件。

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={null}
  import { Sandbox } from 'ppio-sandbox/code-interpreter'

  const sandbox = await Sandbox.create()

  // 在沙箱内创建一个文件用于测试
  const filePathInSandbox = '/tmp/test-file'
  await sandbox.files.write(filePathInSandbox, "test-file-content")


  const fileContent = await sandbox.files.read(filePathInSandbox)
  console.log(fileContent)

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={null}
  from ppio_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create()

  # 在沙箱内创建一个文件用于测试
  file_path_in_sandbox = '/tmp/test-file'
  sandbox.files.write(file_path_in_sandbox, "test-file-content")

  file_content = sandbox.files.read(file_path_in_sandbox)
  print(file_content)

  sandbox.kill()
  ```
</CodeGroup>

## 写入单个文件

您可以使用 `files.write()` 方法将单个文件写入沙箱文件系统。

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={null}
  import { Sandbox } from 'ppio-sandbox/code-interpreter'
  const sandbox = await Sandbox.create()

  const filePathInSandbox = '/tmp/test-file'

  const result = await sandbox.files.write(filePathInSandbox, "test-file-content")
  console.log(result)

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={null}
  from ppio_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create()

  # 在沙箱内创建一个文件用于测试
  file_path_in_sandbox = '/tmp/test-file'
  result = sandbox.files.write(file_path_in_sandbox, "test-file-content")
  print(result)

  sandbox.kill()
  ```
</CodeGroup>

## 写入多个文件

您也可以使用 `files.write()` 方法将多个文件写入沙箱文件系统。

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={null}
  import { Sandbox } from 'ppio-sandbox/code-interpreter'
  const sandbox = await Sandbox.create()

  const result = await sandbox.files.write([
      { path: '/tmp/test-file-1', data: 'file content 1' },
      { path: '/tmp/test-file-2', data: 'file content 2' }
  ])

  console.log(result)

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={null}
  from ppio_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create()

  result = sandbox.files.write_files([
      { "path": "/tmp/test-file-1", "data": "file content 1" },
      { "path": "/tmp/test-file-2", "data": "file content 2" }
  ])
  print(result)

  sandbox.kill()
  ```
</CodeGroup>
