Write Python. Understand every error.

Open the editor

Error guide · Other errors

FileNotFoundError: No such file or directory

The program tried to open a file that does not exist.

Example

f = open("marks.txt")
print(f.read())

What Python says

FileNotFoundError: [Errno 2] No such file or directory: 'marks.txt'

As printed by Python 3.12. Other versions may word it slightly differently.

Try this code in Py-CRAFT

Why it happens

Opening a file for reading needs the file to exist already, with exactly that name. Opening with mode "w" creates it.

How to fix it

f = open("marks.txt", "w")
f.write("72 85 90")
f.close()
f = open("marks.txt")
print(f.read())

Output: 72 85 90

Tips

Other common errors

Share this page: