-1

I have the following code in jupyter notebook, using python. I get the error when I run it saying "FileNotFoundError" but all of the files are in a labeled folder.

file_path=os.path.dirname(os.path.abspath("__file__"))
df=pd.read_csv(file_path+ "\\score_NFL.csv",encoding="utf-8")
teams=pd.read_csv(file_path+"\\nfl_teams.csv",encoding='utf-8')
games_elo=pd.read_csv(file_path+"\\nfl_games3.csv",encoding="utf-8")
games_elo18=pd.read_csv(file_path + "\\nfl_games_2019_1.csv",encoding="utf-8")
numbers25
  • 17
  • 5

1 Answers1

0

You don't want to have quotes around __file__. It refers to a special object created when running a script or using an imported module (see the answers here for details). Your first line should be

file_path=os.path.dirname(os.path.abspath(__file__))

However, you state in your question that you're attempting to run this code in a Jupyter notebook. In an instance like this, similar to using the REPL on the command line, __file__ is not defined because you're not running the script from a file - it's interactive.

This method will work if you save your code in a .py file and run it from the directory containing your CSV files. At the same time, if you're running the code from the same directory, you don't need to go through all the hassle of creating a full absolute path to the CSVs, you can simply use

df = pd.read_csv("score_NFL.csv", encoding="utf-8")

for example.

MattDMo
  • 100,794
  • 21
  • 241
  • 231