Errno::ENOENT
RubyERRORNotableFilesystem

No such file or directory

Quick Answer

Check File.exist? before accessing a file, or rescue Errno::ENOENT to handle missing files gracefully.

What this means

Raised when a file or directory operation references a path that does not exist. The most common filesystem error in Ruby. Maps to POSIX errno 2 (ENOENT).

Why it happens
  1. 1Opening, reading, or deleting a file at a path that does not exist
  2. 2Directory component of a path does not exist
  3. 3Hardcoded path that differs between environments

Fix

Rescue with a fallback

Rescue with a fallback
def read_config(path)
  File.read(path)
rescue Errno::ENOENT
  '{}'   # return empty JSON as default
end

Why this works

Rescuing Errno::ENOENT lets you provide a sensible default when the file is absent.

Code examples
Reproducing the errorruby
File.open('/nonexistent/file.txt')
# Errno::ENOENT: No such file or directory - /nonexistent/file.txt
Guard with File.exist?ruby
if File.exist?(path)
  File.read(path)
else
  puts "#{path} not found"
end
Rescue Errno::ENOENTruby
begin
  FileUtils.rm(temp_file)
rescue Errno::ENOENT
  # already deleted — ignore
end
Sources
Official documentation ↗

Ruby Core Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Ruby errors