Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

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);
with Ada.Directories;
B : constant Boolean := Ada.Directories.Exists (FP);
#include <unistd.h>
#include <stdbool.h>
bool b = (access(_fp, F_OK) == 0) ? true : false;
(require '[clojure.java.io :as io])
(let [b (.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;
}
import java.io.File;
boolean b = new File(fb).exists();
import java.io.File;
b = 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();

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