Logo

Programming-Idioms

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

let b = std::path::Path::new(fp).exists();
with Ada.Directories;
B : constant Boolean := Ada.Directories.Exists (FP);
#include <unistd.h>
#include <stdbool.h>
bool b = access(_fp, F_OK) == 0
(require '[clojure.java.io :as io])
(.exists (io/file fp))
#include <filesystem>
bool b = std::filesystem::exists(fp);
bool b = File.Exists(fp);
import std.file;
bool b = fp.exists;
import 'dart:io';
var b = await File(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 { access } from 'fs/promises';
let b = true;
try {
	await access(fp);
} catch {
	b = false;
}
try {
	Deno.statSync(fp)
} catch(_e) {console.error("File does not exist.")}
import java.io.File;
boolean b = new File(fb).exists();
import java.io.File;
b = 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)

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