How to fix FileNotFoundError in Python Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report FileNotFoundError is a built-in Python exception that is raised when an operation such as reading, writing or deleting is attempted on a file that does not exist at the specified path. It is a subclass of OSError and commonly occurs when functions like open(), os.remove() and similar file-handling methods are used without first checking if the file is actually present. Example: Python file = open("abc.txt") OutputHangup (SIGHUP)Traceback (most recent call last): File "/home/guest/sandbox/Solution.py", line 1, in <module> file = open("abc.txt")FileNotFoundError: [Errno 2] No such file or directory: 'abc.txt'SolutionsLet’s look at a few simple ways to fix FileNotFoundError and make sure your program keeps running smoothly even if a file is missing.1. Check if file exits before opening Python provides the os.path.exists() method from the built-in os module to check whether a file exists at a given path. Python import os fp = "abc.txt" if os.path.exists(fp): with open(fp, "r") as f: data = f.read() else: print("Not found!") OutputNot found! 2. Using try-except block Try block is a reliable way to handle FileNotFoundError. It lets your program attempt to open the file, and if the file doesn’t exist, the except block gracefully handles the error without crashing the program. Python try: with open("abc.txt", "r") as file: data = file.read() except FileNotFoundError: print("Not found") OutputNot found 3. Use absolute file path Absolute path specify the exact location of the file on your system, removing any ambiguity about where Python should look for it. Python import os print("Current directory:", os.getcwd()) fp = "/absolute/path/to/abc.txt" # file path with open(fp, "r") as file: data = file.read() 4. Create the file if missing If the file does not exist, using write mode ("w") in Python will automatically create it. This is useful when you want to ensure the file exists, even if it wasn't there before. Python with open("abc.txt", "w") as file: file.write("This file has been created!") Outputabc.txt file Comment More infoAdvertise with us Next Article How to fix FileNotFoundError in Python S shetyeanuja2000 Follow Improve Article Tags : Python Geeks Premier League Geeks Premier League 2023 Practice Tags : python Similar Reads How to Fix "EOFError: EOF when reading a line" in Python The EOFError: EOF when reading a line error occurs in Python when the input() function hits an "end of file" condition (EOF) without reading any data. This is common in the scenarios where input() expects the user input but none is provided or when reading from the file or stream that reaches the EO 3 min read How to copy file in Python3? Prerequisites: Shutil When we require a backup of data, we usually make a copy of that file. Python supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files. Here we will learn how to copy a file using Pyt 2 min read How to read multiple text files from folder in Python? Reading multiple text files from a folder in Python means accessing all the .txt files stored within a specific directory and processing their contents one by one. For example, if a folder contains three text files, each with a single line of text, you might want to read each fileâs content and proc 3 min read How To Fix Valueerror Exceptions In Python Python comes with built-in exceptions that are raised when common errors occur. These predefined exceptions provide an advantage because you can use the try-except block in Python to handle them beforehand. For instance, you can utilize the try-except block to manage the ValueError exception in Pyth 4 min read How to fix: "fatal error: Python.h: No such file or directory" The "fatal error: Python.h: No such file or directory" error is a common issue encountered when compiling C/C++ code that interacts with Python. This error occurs when the C/C++ compiler is unable to locate the Python.h header file, which is part of the Python development package required for compil 5 min read EnvironmentError Exception in Python EnvironmentError is the base class for errors that come from outside of Python (the operating system, file system, etc.). It is the parent class for IOError and OSError exceptions. exception IOError - It is raised when an I/O operation (when a method of a file object ) fails. e.g "File not found" or 1 min read Handling EOFError Exception in Python In Python, an EOFError is raised when one of the built-in functions, such as input() or raw_input() reaches the end-of-file (EOF) condition without reading any data. This commonly occurs in online IDEs or when reading from a file where there is no more data left to read. Example:Pythonn = int(input( 4 min read How to Get directory of Current Script in Python? A Parent directory is a directory above another file/directory in a hierarchical file system. Getting the Parent directory is essential in performing certain tasks related to filesystem management. In this article, we will take a look at methods used for obtaining the Parent directory of the curren 4 min read How To Fix Modulenotfounderror And Importerror in Python Two such errors that developers often come across are ModuleNotFoundError and ImportError. In this guide, we'll explore what these errors are, the common problems associated with them, and provide practical approaches to resolve them. What are ModuleNotFoundError and ImportError?ModuleNotFoundError: 3 min read How to iterate over files in directory using Python? Iterating over files in a directory using Python involves accessing and processing files within a specified folder. Python provides multiple methods to achieve this, depending on efficiency and ease of use. These methods allow listing files, filtering specific types and handling subdirectories.Using 3 min read Like