Python filenotfounderror — How to fix filenotfounderror in Python

While working with files in Python, one of the most common and repeatable errors often occurs in filenotfounderror.

This error message, filenotfounderror, will be thrown by the interpreter when we are trying to execute a line of code/command that requires a file that the system cannot find.

The main reason for this error is specifying the path of the located file (or) wrong file name (or) wrong extension. Let’s discuss all the possible ways & scenarios to fix the filenotfounderrror.

Below is a simple program that throws the filenotfounderror message.

# import required libraries
import pandas as pd
# load dataset
data=pd.read_csv('Fish.csv')
# print data
data

Output:

—————————————————————————

FileNotFoundError                         Traceback (most recent call last)

<ipython-input-1-721f4dfed070> in <module>

      1 import pandas as pd

—-> 2 data=pd.read_csv(‘Fish.csv’)

      3 data

________________________________________________________________________

7                                                                                                                      frames

________________________________________________________________________

/usr/local/lib/python3.7/dist-packages/pandas/io/common.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)

    705                 encoding=ioargs.encoding,

    706                 errors=errors,

–> 707                 newline=””,

    708             )

    709         else:

FileNotFoundError: [Errno 2] No such file or directory: ‘Fish.csv’

Explanation: The error is that the file Fish.csv is not in the same directory/folder in which the actual program exists/runs. Below are the Various methods/scenarios to fix the filenotfounderror.

Also ReadHow To Open a File in Python

How to fix file not found error in Python

how to fix filenotfounderror in python

Also ReadUnderstand Python Lambda Functions With Examples

Method 1: Specify the file path

When we run a program in Terminal (or) IDE, If we specify only the filename in the read_csv() method, then Python Interpreter only searches for the file present in the current directory/folder where the code exists/runs. If the file & code are present in the same folder/library, it won’t throw an error.

If the file is in a different folder, we need to specify the file’s complete path so that the interpreter can go to that folder/directory and pick the required file.

Code:

# import required libraries
import pandas as pd
# load dataset
data=pd.read_csv(r'C:\Users\v.akhil\Desktop\Fish.csv')
# print data
data

Output:

fix filenotfounderror by specifying file path

Explanation: Here, the dataset Fish.csv is in the Desktop folder, which is different from where we run the code. So we explicitly specified the path of the file to load the file.

You can notice that while specifying the file’s path, we used r at the start, i.e., read_csv(r’C:\Users\v.akhil\Desktop\Fish.csv’) because it converts the simple string into a raw string.

If we don’t specify the r before the file path, then the interpreter will consider the complete path of a file as a regular string instead of a file path which results in an error.

Also ReadPython Zip() Function Tutorial With Code Examples

Method 2: Use a text file to run the Python script

We can also execute the code in a file in a particular python folder by following the two steps below.

By moving into the directory where the text file is present, which needs to be executed using the command cd directory_name

After navigation, Execute the Python script by the command – Python filename.

To omit the filenotfounderror, we need to ensure that the dataset (file used in code) and code file should be present in the same directory.

Code:

# import required libraries
import pandas as pd

# load dataset
data=pd.read_csv('Fish.csv')

# print data
print(data)

 

using txt file to run python script - fix filenotfounderror
Both text file (code) & data (CSV file) are present in the same folder (Desktop)
root folder to desktop using cd command
Moved from the root folder to the desktop using the cd command

Output:

output of executing python code using text file

This method is used by programmers when they deal with multiple files in the same program. So at every time, they can specify the file name without the entire file path.

Note: This approach can even be followed when using any integrated development environment like PyCharm for programming. As in this method, we created a folder as a placeholder for all files, i.e., Text File (consists of code) and CSV file (dataset).

Similarly, in IDE like Pycharm, we will create a project and place the required files in the same project to avoid specifying the complete file path & filenotfounderror.

Also ReadQuicksort Implementation in Python

Method 3: Using Google collab online IDE

Google collab is one of the popular online IDE used by many people for coding. While coding in the collab, even if we specify the file’s complete path, the IDE will throw filenotfounderror.

# import required libraries
import pandas as pd
# load dataset
data=pd.read_csv(r'C:\Users\v.akhil\Desktop\Fish.csv')

Output:

FileNotFoundError                         Traceback (most recent call last)

<ipython-input-3-ef8bf547a048> in <module>

      2 import pandas as pd

      3 # load dataset

—-> 4 data=pd.read_csv(r’C:\Users\v.akhil\Desktop\Fish.csv’)

_____________________________________________________________________________

7                                                                                                                               Frames

_____________________________________________________________________________

/usr/local/lib/python3.7/dist-packages/pandas/io/common.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)

    705                 encoding=ioargs.encoding,

    706                 errors=errors,

–> 707                 newline=””,

    708             )

    709         else:

 

FileNotFoundError: [Errno 2] No such file or directory: ‘C:\\Users\\v.akhil\\Desktop\\Fish.csv’

So to get rid of this error in Google collab. First, we must upload the file into the notebook using GUI or code.

From GUI, upload files by clicking on the highlighted icon below.

filenotfounderror on google colab notebook

Also ReadHow To Create a Python Web Server

We can also upload the file into the notebook using very few lines of code which are given below:

from google.colab import files
files.upload()

The notebook will allow us to upload the files by running the above code.

google colab file upload

After Adding/uploading files, we can directly specify the filename in the read_csv() method without any path to use the dataset for manipulation/processing as per need.

Code:

# import required libraries
import pandas as pd
 
# load dataset
data=pd.read_csv('Fish.csv')
data

Output:

how to fix filenotfounderror in python

Scroll to Top