[SOLVED] Python filenotfounderror – A Quick Guide

python_filenotfounderror-title.png

In this article, we will be addressing a very common error – filenotfounderror – in Python . If you have worked with Python before, you would have faced this situation too, where you write all your code, maintain proper indents in it, put comments line, double-check for mistypes, and after making sure that everything is right and in its place, you run the code and end up getting ‘filenotfounderror’ in the compiler line.

Frustrating, isn’t it? Don’t worry, we will make sure to cover all possible ways to resolve this problem so that you do not encounter it ever again.

Also read: Handling IOErrors in Python – A Complete Guide

What is filenotfounderror

It is a system message that the compiler throws when you are trying to execute a command that requires a file that the system cannot find. It can be for various reasons like – wrong file path specified, the file is present in another directory, or an extension error. We will address the points in this article. But let’s first recreate the problem in our system.

We will be writing a program to load a .csv file into a pandas data frame and then print that data frame.

import pandas as pd

df=pd.read_csv("nifty 2020 crash")
print(df)

Filenotfounderror_image.png
Filenotfounderror

How to Fix the Python filenotfounderror?

When you run your python code in the terminal, it searches for the file in the root directory from where the terminal is running. A common misconception people have is that when you run a python code to read a file, the terminal searches that file in the whole computer, which is incorrect.

All your files that are needed for your program should be present in the root directory from where the terminal is activated.

This problem can be resolved in two ways:

Method 1: Specifying the complete file path

When we run our program, we state the file name in the program. The compiler searches for it in the root directory and throws errors. The solution to this problem is specifying the whole file path in the code.

import pandas as pd

df = pd.read_csv(r"C:\Users\Win 10\Desktop\python_errorsolving\nifty 2020 crash.csv")
print(df)
solving_filenotfound_method-1.png

Note: Observe, while specifying the file path, we added an r before writing the path, pd.read_csv(r"C:\.......). It is used to convert simple strings to raw strings. If we do not add r before specifying our file path, the system is gonna treat that line of code as a normal string input instead of a file path.

Method 2: Using a .txt file to run Python script

In this method, we use a very simple but effective approach to this problem. We write our code in a .txt file and store it in the directory where the file we require is present. When we run this .txt file with python, the compiler searches for that file only in that directory. This method does not require us to specify the whole file path but we need to make sure that the terminal has been run from the right directory.

To illustrate this example, we have created a directory in our desktop named, ‘python_errorsolving’. This directory contains two files, – the .txt file containing python codes and the .csv file we needed for our code.

method2_notepad_screenshot.png

To run this file from the terminal, go to the directory manually using cd, and then run this file with syntax python error_crt.txt or whatever your file name is.

method2_dos_screenshot.png

As you can see, this method does not require us to specify the whole file path. It is useful when you have to work with multiple files because specifying the whole path for each specific file can be a tedious job.

Method 3: Workaround for filenotfounderror

This is not a solution but rather a workaround for this problem. Suppose you are in a certain situation where the file path is the same but you have to load consecutive different files. In that case, you can store your filename and file path in two different variables and then concatenate them in a third variable. This way you can make combinations of multiple different files, and then load them easily.

To illustrate this solution, we will create a .txt file, with the codes:

import pandas as pd

filename = "nifty 2020 crash.csv"
filepath = "C:\\Users\\Win 10\\Desktop\\python_errorsolving\\"
file = filepath+filename

df = pd.read_csv(file)
print(df)

alterate_method_dos-screenshot.png

Using an IDE to fix the filenotfounderror

Integrated development environments or IDE’s are a great way to manage our files and environment variables. This helps to create virtual environments for your codes so that the needed libraries and environment variables do not interact with our other projects. In this section, we will create a project in PyCharm IDE, and see how easily we can store and interact with our files.

To demonstrate this example, we have created a .csv file containing school records, and it is named ‘book1.csv’. To import it in PyCharm, follow these steps:

Step 1: Go to File>new project…>give a file name>create.

Step 2: Copy your .csv file and paste it into that project.

method_ide_paste_screen-shot.png

Once you paste the file, you can directly access that file with your codes, without having to specify the whole path. You can simply work with the filename.

import pandas as pd

df = pd.read_csv('Book1.csv', sep='|')
print(df)

Result:

method_ide_paste_screen-shot2.png

Conclusion

In this article, we have observed the different cases where the system cannot locate your files. We also looked at the different solutions that are possible to these problems, from manually specifying paths, to using IDE’s for a better outcome. I hope this article solved your problem