# read()

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/local-files/read/
Language: zh

`files.read(file) → Promise<Item & { content }>`

读取整个文件。 文本以字符串形式返回, 其它任何内容作为缓冲, 由扩展名决定 。

Status: free · whole file · string or Buffer

## 参数 {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `file` | string | 必填 | 要读取的文件, 相对于工作空间 。 |

```javascript
const file = await pc.files.read("Launch plan/brief.md");
// {
//   name: "brief.md",
//   path: "Launch plan/brief.md",
//   type: "file",
//   size: 1284,
//   mimeType: "text/markdown",
//   modifiedAt: "2026-10-07T09:12:44.000Z",
//   content: "# Brief\n\nShip in March."     // a string
// }
```

## 哪一个你得到 {#content}

| MIME 类型 | 内容为内容 |
| --- | --- |
| `text/*` | 字符串 ()UTF-8) |
| `application/json` · `application/xml` · `application/yaml` | 字符串 |
| `text/javascript` · `text/typescript` | 字符串 |
| 所有其他——图像、PDFs、音频、档案 | a 缓冲器 |

> **延期决定, 而不是字节**
>
> A A A JSON 以.dat 保存的有效载荷作为缓冲返回,而以.txt 保存的二进制当回一个被操纵的字符串。 命名文件, 或测试文件 Buffer.isBuffer(content) 在使用结果之前。

```javascript
const file = await pc.files.read(path);
const text = Buffer.isBuffer(file.content) ? file.content.toString("utf8") : file.content;
```

## 读了整个故事 {#whole}

没有偏移、 无长度和流流: 文件被装入内存。 大的, 读取 Node的 自己的文件系统 API 使用 `list()` 以便他们找到自己的道路, `size` 事先告诉你你将要装载的东西

## 错误 {#errors}

| 代码 | 状态 | 触发条件 |
| --- | --- | --- |
| path_not_found | 404 | 此路径没有文件 。 |
| not_a_file | 400 | 路径名称文件夹 - 使用 `list()`. |
| invalid_path | 400 | 绝对,在工作空间之外,或者工作空间根之外。 |
| local_files_require_node | 400 | 运行环境没有文件系统。 |
| storage_not_on_this_machine | 409 | 另一台电脑的储藏室 |

```javascript
import { IaGenifyError } from "iagenify";

async function readOrNull(files, path) {
  try {
    return await files.read(path);
  } catch (err) {
    if (err instanceof IaGenifyError && (err.code === "path_not_found" || err.code === "not_a_file")) return null;
    throw err;
  }
}
```

- [经纪人的读](/zh/sdk/agent/tools/read): 代理人看到什么——转弯后附有PDF、图像、音频和视频。
- [本地文件](/zh/sdk/ref/local-files): 工作空间、 路径规则、 以及每个方法返回的形状 。

```python
file = pc.files.read("Launch plan/brief.md")
# {
#   "name": "brief.md",
#   "path": "Launch plan/brief.md",
#   "size": 1284,
#   "content": "# Brief\n\nShip in March."     # a str
# }
```

```python
file = pc.files.read(path)
text = file["content"].decode("utf-8") if isinstance(file["content"], bytes) else file["content"]
```

```python
from iagenify import IaGenifyError

def read_or_none(files, path):
    try:
        return files.read(path)
    except IaGenifyError as error:
        if error.code in ("path_not_found", "not_a_file"):
            return None
        raise
```
