File.exists
func File.exists(path: string): boolFile.exists reports whether anything exists at path that File.stat can reach, a directory included. It follows a symbolic link, so a link that leads nowhere is false, and it is false when stat is Err for any reason, such as a directory on the way that cannot be searched.
Details
truefor a file and a directory alike;File.stattells them apart.- The answer can be stale by the time you use it. To create a file only when nothing is there, write it with
replace: falseinstead of checking first.
Basic examples
println(File.exists("notes.txt"))
println(File.exists("missing.txt"))true
falseScope
A directory exists too:
println(File.exists("logs"))
println(File.exists("logs/today.log"))true
trueFile.exists follows a symbolic link. A link to a file is true; a link whose target is missing is false, even though the link itself is there, as File.stat with follow: false shows:
try File.symlink("notes.txt", "current")
defer { File.remove("current") }
try File.symlink("gone.txt", "broken")
defer { File.remove("broken") }
println(File.exists("current"))
println(File.exists("broken"))
println(File.stat("broken", follow: false).isOk())true
false
trueApplications
Use the first configuration file that is present, or the defaults when there is none:
let candidates = ["app.local.toml", "app.toml", "/etc/app/app.toml"]
let config = candidates.find({ File.exists(x) })
println("config: ${config ?? "built-in defaults"}")config: app.tomlTell an existing report apart from other failures after a write with replace: false. The Err does not say why the write failed, so check File.exists after it:
func save(path: string, text: string): Result[void, Error] {
let written = File.write(path, text, replace: false)
if written.isOk() || !File.exists(path) { return written }
Result.fail("${path} is already there; remove it to write a new one")
}
println(save("report.txt", "today"))Err{ error: Error{ message: "report.txt is already there; remove it to write a new one" } }Properties and relations
A file exists after a successful write, and no longer after a successful remove:
try File.write("draft.txt", "first line")
assert(File.exists("draft.txt"))
try File.remove("draft.txt")
assert(!File.exists("draft.txt"))Possible issues
Checking and then writing is two steps, and another program can create the file between them. Let replace: false decide in one step instead:
let output = "out.txt"
// Racy: the file can appear after the check.
if !File.exists(output) { try File.write(output, "first") }
// One step: Err when anything is at the path.
println(File.write(output, "second", replace: false))Err{ error: Error{ message: "open out.txt: file exists" } }A dangling link is not there for File.exists, but it still occupies the name, so a write with replace: false refuses it:
try File.symlink("gone.txt", "broken")
defer { File.remove("broken") }
println(File.exists("broken"))
println(File.write("broken", "text", replace: false))false
Err{ error: Error{ message: "open broken: file exists" } }true does not mean the path is a file. Reading a directory is an Err:
if File.exists("logs") {
println(File.read("logs"))
}Err{ error: Error{ message: "read logs: is a directory" } }See also
File.stat · File.write · File.symlink · File.realPath · File.remove · File.mkdir
