# os.FileExists
> 报告文件或目录是否存在。
- 官方英文原文：https://gohugo.io/functions/os/fileexists/
- 本页规范地址：https://hugozh.cn/functions/os/fileexists/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：os.FileExists PATH
- 返回类型：bool
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
`os.FileExists` 函数先尝试相对于项目目录的根解析路径。如果找不到匹配的文件或目录，它会尝试相对于 [`contentDir`][] 解析路径。路径开头的分隔符（`/`）是可选的。

目录结构如下：

```tree
content/
├── about.md
├── contact.md
└── news/
    ├── article-1.md
    └── article-2.md
```

该函数返回以下值：

```go-html-template
{{ fileExists "content" }} → true
{{ fileExists "content/news" }} → true
{{ fileExists "content/news/article-1" }} → false
{{ fileExists "content/news/article-1.md" }} → true
{{ fileExists "news" }} → true
{{ fileExists "news/article-1" }} → false
{{ fileExists "news/article-1.md" }} → true
```

[`contentDir`]: /configuration/all/#contentdir

