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.

uses sysutils;
b := FileExists(fp);
#include <unistd.h>
#include <stdbool.h>
bool b = (access(_fp, F_OK) == 0) ? true : false;
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);
import java.io.File;
boolean b = new File(fb).exists();
$b = file_exists($fp);
my $b = -f $fp;
import os
b = os.path.exists(fp)
from pathlib import Path
b = Path(fp).exists()
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

programming-idioms.org

Related idioms