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.
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
- Check the file name, including the extension such as
.txt. - Create or write the file before reading it.