Occurs when the file exists, but Python can't find it from the current working directory.
with open("data.txt", "r") as f:
content = f.read()
print(content)FileNotFoundError: [Errno 2] No such file or directory: 'data.txt'
import os
base_dir = os.path.dirname(__file__)
file_path = os.path.join(base_dir, "data.txt")
with open(file_path, "r") as f:
content = f.read()
print(content)Thought the file was in the same folder, but Python was running from a different location.