This language bar is your friend. Select your favorite languages!

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 never do this and then in the next instruction assume the result is still valid, this is a race condition on any multitasking OS.

#include <unistd.h>
#include <stdbool.h>
bool b = (access(_fp, F_OK) == 0) ? true : false;
(require '[ :as io])
(let [b (.exists (io/file fp))]
bool b = File.Exists(fp);
import std.file;
bool b = fp.exists;
  inquire (file=fp,exist=b)
import "os"
_, err := os.Stat(fp)
b := !os.IsNotExist(err)
import System.Directory
b = doesFileExist fp
const fs = require('fs');
const b = fs.existsSync(fp);
boolean b = new File(fb).exists();
$b = file_exists($fp);
uses sysutils;
b := FileExists(fp);
my $b = -f $fp;
from pathlib import Path
b = Path(fp).exists()
import os
b = os.path.exists(fp)
b = File.exist?(fp)
let _b = std::path::Path::new(_fp).exists();

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

Related idioms