Otter
Snippets
Otter
Upstream Otter 1.0.1 Tested
Safe File Read with Fallback
Verify file existence before reading to return fallback content when absent.
Architecture & Runtime Preview
Otter 1.0.1Zero-dependency Otter 1.0.1 snippet (Files & I/O). Copy the parameterized template or run the self-contained example.
- 1Category: Files & I/O
- 2Attribution: Otter Core Language Spec (D20)
- 3Zero external pack dependencies — paste directly into any .ot file
Source Code (2 files)
if file "${filePath}" exists
read "${filePath}" into ${content}
otherwise
${content} is "${fallbackDefault}"
.
testPath is "snippet_test_read.txt"
write "version-2026.1" to testPath
if file testPath exists
read testPath into appVersion
otherwise
appVersion is "fallback-version"
.
say "App Version:" appVersion
if appVersion is not "version-2026.1"
fail with "file-read-safe existing file read mismatch"
.
delete file testPath
if file "non_existent_file_999.txt" exists
read "non_existent_file_999.txt" into missingVal
otherwise
missingVal is "safe-fallback"
.
if missingVal is not "safe-fallback"
fail with "file-read-safe fallback branch mismatch"
.
Installation & Usage
otter snippet inspect file-read-safe
Copy template.ot directly into your .ot source file, or run example.ot with 'otter run example.ot'.