hugo/content/en/functions/os/FileExists.md
Bjørn Erik Pedersen 9b0050e9aa Squashed 'docs/' content from commit 5c085a37b
git-subtree-dir: docs
git-subtree-split: 5c085a37b297bf12f59efeaae591418ec025c10d
2024-01-27 10:48:33 +01:00

41 lines
1.2 KiB
Markdown

---
title: os.FileExists
description: Reports whether the file or directory exists.
categories: []
keywords: []
action:
aliases: [fileExists]
related:
- functions/os/Getenv
- functions/os/ReadDir
- functions/os/ReadFile
- functions/os/Stat
returnType: bool
signatures: [os.FileExists PATH]
aliases: [/functions/fileexists]
---
The `os.FileExists` function attempts to resolve the path relative to the root of your project directory. If a matching file or directory is not found, it will attempt to resolve the path relative to the [`contentDir`](/getting-started/configuration#contentdir). A leading path separator (`/`) is optional.
With this directory structure:
```text
content/
├── about.md
├── contact.md
└── news/
├── article-1.md
└── article-2.md
```
The function returns these values:
```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
```