Python 3.3+Exceptionsexception
FileNotFoundError
built-in exceptions
Raised on an attempt to open or use a file that does not exist. A subclass of OSError.
Syntax
raise FileNotFoundError('файл не найден')
Usually the culprit is not a missing file but a relative path: it resolves against the current working directory, not the script's folder, so the same code breaks when run from elsewhere. In write mode the meaning shifts — the file itself may be absent, but the directory you write into must already exist. Catch this specific class rather than bare OSError, or permission denials and unrelated failures land in the same branch.
Examples
open('no_file.txt') # FileNotFoundError
try:
with open('data.txt') as f:
text = f.read()
except FileNotFoundError:
print('файл не найден')
See also
Open in the full glossarydocs.python.orgReport an inaccuracy