Cannot get Jupyter to read the CSV file - error - file not found

Viewed 1571

I started with a tutorial that showed how to import Pandas and now I want to read the CSV file. I feel like I have tried everything. I tried all different types of encoding commands, that doesn't seem to do anything - I keep getting the same error - that the CSV file does not exist. I tried including the full path name, and saving the CSV file in different places (currently on my desktop). Nothing. Would someone please explain to me what I could be doing wrong in layman terms, as I have no clue. It feels like it could be something so simple!

# Pandas for managing datasets
import pandas as pd
# Matplotlib for additional customization
from matplotlib import pyplot as plt
%matplotlib inline
# Seaborn for plotting and styling
import seaborn as sns
# Read dataset
df = pd.read_csv('Pokemon.csv', index_col=0)

Traceback

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-9-b0b02dbb5c90> in <module>
      1 # Read dataset
----> 2 df = pd.read_csv('Pokemon.csv', index_col=0)

~\anaconda3\lib\site-packages\pandas\io\parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
3 Answers

Generally anytime you get a FileNotFoundError, it's an issue with your program finding the file.

You can be sure you have the right file path by navigating to the file on your computer, as though you were going to open it. Then right-click and select Properties. I can't speak for Linux or Mac, but on Windows you'll find the file path under the "Security" tab in a field called "Object Name". You can copy/paste that file path into your program like this:

pd.read_csv(r'(paste your file path here, and remove parentheses)')

Also, be sure to include the r before your file path string. That will tell Python to escape the backslashes in Windows file paths.

I recommend checking to see what where you have opened your Jupyter Notebook. Open the Notebook in the same directory that 'Pokemon.csv' is in or put your csv file in the directory that you opened your Jupyter notebook. I know you said that you have put the full path name in, but that shouldn't be necessary.

Check your current working directory:

import os
os.getcwd()

Sample Output:

'C:\\Users\\WORKSTATION\\Downloads'

if the file is in this folder, then you will be able to do:

df = pd.read_csv('Pokemon.csv', index_col=0)

else, get the directory of your file, then do: (Sample directory, say in Documents)

df = pd.read_csv('C:\\Users\\WORKSTATION\\Documents\\Pokemon.csv', index_col=0)

or

df = pd.read_csv(r'C:\Users\WORKSTATION\Documents\Pokemon.csv', index_col=0)
Related