本地文件

read()

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

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

freewhole filestring or Buffer
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

参数

filestring必填

要读取的文件, 相对于工作空间 。

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
// }
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
# }

哪一个你得到

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) 在使用结果之前。

const file = await pc.files.read(path);
const text = Buffer.isBuffer(file.content) ? file.content.toString("utf8") : file.content;
file = pc.files.read(path)
text = file["content"].decode("utf-8") if isinstance(file["content"], bytes) else file["content"]

读了整个故事

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

错误

代码状态触发条件
path_not_found404此路径没有文件 。
not_a_file400路径名称文件夹 - 使用 list().
invalid_path400绝对,在工作空间之外,或者工作空间根之外。
local_files_require_node400运行环境没有文件系统。
storage_not_on_this_machine409另一台电脑的储藏室
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;
  }
}
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