# list()

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

`files.list(dir?, { recursive? }) → Promise<Item[]>`

文件夹的内容: 先是文件夹, 然后是文件, 以名称命名。 使用递归, 树上的每一文件 。

Status: free · hidden entries omitted · 500 items at most

## 参数 {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `dir` | string |  | 要读取的文件夹, 相对于工作空间。 空( 默认) 读取工作空间本身 。 |
| `recursive` | boolean | false | 整棵树走完,然后回来 **只保存文件**,他们完全的相对路径。 |

## 一级 {#flat}

```javascript
await pc.files.list();
// [
//   { name: "Launch plan", path: "Launch plan", type: "folder", size: null,  mimeType: null,            modifiedAt: "…" },
//   { name: "notes.md",    path: "notes.md",    type: "file",   size: 1284,  mimeType: "text/markdown", modifiedAt: "…" }
// ]

await pc.files.list("Launch plan");
// [ { name: "brief.md", path: "Launch plan/brief.md", type: "file", … } ]
```

文件夹在文件之前出现, 每个组会按路径排序。 此顺序是稳定的, 这样列表可以随着它到达时显示 。

## 整棵树 {#recursive}

`recursive: true` 不仅它去的深度:文件夹不再是他们自己的条目。你得到的文件,每个文件都携带着您可以直接通过的路 `read()`.

```javascript
const files = await pc.files.list("", { recursive: true });

// Every file, no folder entries
const markdown = files.filter((f) => f.mimeType === "text/markdown");
const bytes = files.reduce((total, f) => total + f.size, 0);
```

> **上限为500件,没有报告**
>
> 单步停止 。 单步停止只要它有500个条目。 没有旗帜可以这么说, 而在大型工作空间上很重要 : 要通过更多, 一次列出文件夹而不是根递现 。

## 并非结果 {#hidden}

以点开头的名称在每级别跳过条目的条目。 `dir` 也让垃圾桶能保持畅通,

## 错误 {#errors}

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

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

async function listOrEmpty(files, dir) {
  try {
    return await files.list(dir);
  } catch (err) {
    if (err instanceof IaGenifyError && err.code === "path_not_found") return [];
    throw err;
  }
}
```

- [read()](/zh/sdk/ref/local-files/read): 读读您刚刚列出的文件之一。
- [本地文件](/zh/sdk/ref/local-files): 工作空间、 路径规则、 以及每个方法返回的形状 。

```python
pc.files.list()
# [
#   {"name": "Launch plan", "path": "Launch plan", "type": "folder", "size": 0},
#   {"name": "notes.md",    "path": "notes.md",    "type": "file",   "size": 1284}
# ]

pc.files.list("Launch plan")
# [{"name": "brief.md", "path": "Launch plan/brief.md", "type": "file", …}]
```

```python
files = [f for f in pc.files.list("", recursive=True) if f["type"] == "file"]

# Every file, no folder entries
markdown = [f for f in files if f["path"].endswith(".md")]
total_bytes = sum(f["size"] for f in files)
```

```python
from iagenify import IaGenifyError

def list_or_empty(files, directory):
    try:
        return files.list(directory)
    except IaGenifyError as error:
        if error.code == "path_not_found":
            return []
        raise
```
