Logo

Programming-Idioms

  • Kotlin
  • Go

Idiom #144 Check if file exists

Set boolean b to true if file at path fp exists on filesystem; false otherwise.

Beware that you should not do this and then in the next instruction assume the result is still valid, this is a race condition on any multitasking OS.

import "os"
_, err := os.Stat(fp)
b := !os.IsNotExist(err)

There's no specific existence check func in standard library, so we have to inspect an error return value.
import java.io.File;
b = File(fb).exists()
with Ada.Directories;
B : constant Boolean := Ada.Directories.Exists (FP);

New implementation...
< >
programming-idioms.org