file_exists
file_exists(string $filename): bool
Checks whether a file or directory exists.
C C23
#include <stdio.h>
#include <unistd.h>
int main(void) {
FILE *fp = fopen("snippet_fe_demo.txt", "w");
fputs("x", fp);
fclose(fp);
printf("%d %d\n", access("snippet_fe_demo.txt", F_OK) == 0,
access("snippet_fe_missing.txt", F_OK) == 0);
return 0;
}
1 0
C++ C++26
#include <filesystem>
#include <fstream>
#include <iostream>
int main() {
{ std::ofstream f("snippet_fe_demo.txt"); f << "x"; }
using namespace std::filesystem;
std::cout << std::boolalpha << exists("snippet_fe_demo.txt") << " "
<< exists("snippet_fe_missing.txt") << "\n";
return 0;
}
true false
C# v11.0.100
File.WriteAllText("snippet_fe_demo.txt", "x");
Console.WriteLine($"{File.Exists("snippet_fe_demo.txt")} {File.Exists("snippet_fe_missing.txt")}");
True False
Go v1.27.4
package main
import (
"fmt"
"os"
)
func main() {
os.WriteFile("snippet_fe_demo.txt", []byte("x"), 0644)
_, err1 := os.Stat("snippet_fe_demo.txt")
_, err2 := os.Stat("snippet_fe_missing.txt")
fmt.Println(err1 == nil, err2 == nil)
}
true false
Java v27.0.1
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws IOException {
Path p = Path.of("snippet_fe_demo.txt");
Files.writeString(p, "x");
System.out.println(Files.exists(p) + " " + Files.exists(Path.of("snippet_fe_missing.txt")));
}
}
true false
JavaScript v26.13.1
const fs = require("node:fs");
fs.writeFileSync("snippet_fe_demo.txt", "x");
console.log([fs.existsSync("snippet_fe_demo.txt"), fs.existsSync("snippet_fe_missing.txt")]);
[ true, false ]
PHP v8.6.0
file_put_contents("snippet_fe_demo.txt", "x");
echo json_encode([file_exists("snippet_fe_demo.txt"), file_exists("snippet_fe_missing.txt")]);
[true,false]
Python v3.15.2
from pathlib import Path
p = Path("snippet_fe_demo.txt")
p.write_text("x")
print([p.exists(), Path("snippet_fe_missing.txt").exists()])
[True, False]
Rust v1.99.1
use std::fs;
use std::path::Path;
fn main() {
fs::write("snippet_fe_demo.txt", "x").unwrap();
println!(
"{} {}",
Path::new("snippet_fe_demo.txt").exists(),
Path::new("snippet_fe_missing.txt").exists()
);
}
true false
TypeScript v26.13.1
import * as fs from "node:fs";
fs.writeFileSync("snippet_fe_demo.txt", "x");
console.log([fs.existsSync("snippet_fe_demo.txt"), fs.existsSync("snippet_fe_missing.txt")]);
[ true, false ]
Language differences
C: uses POSIX access(path, F_OK).
C#: File.Exists only matches files (Directory.Exists for directories — both return false for a path pointing at the other kind); every language follows symlinks by default and treats a dangling link as missing, and File.Exists likewise gives False.
Go: no boolean helper — the idiom is os.Stat plus error checking, and production code should use errors.Is(err, fs.ErrNotExist) rather than err == nil, which misclassifies permission errors as a successful lookup; race caution: check-then-open is the TOCTOU pattern — the idiomatic move is to just open and handle the failure (documented explicitly in the Go and Elixir ecosystems).
Java: Files.exists also accepts the optional LinkOption.NOFOLLOW_LINKS to inspect the link itself.
PHP: file_exists returns true for both files and directories.
Python: follows links by default and treats a dangling link as missing — os.path.exists gives False while os.path.lexists inspects the link itself; the official docs also recommend just opening and handling the failure.