0

I am getting this error when trying to print the contents of a CSV file in Python.

Traceback (most recent call last): File "/Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/Player.py", line 5, in with open('player.csv') as csvfile: FileNotFoundError: [Errno 2] No such file or directory: 'player.csv'

4
  • It means that there is no file named player.csv in the same directory as your Python script (i.e. scratches). Commented Nov 28, 2018 at 2:50
  • @Selcuk I don't see a script on my computer called scratches though. I had renamed the file to Player Commented Nov 28, 2018 at 2:58
  • Examine the traceback, you have a directory called /Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/ Commented Nov 28, 2018 at 3:10
  • It means you must put player.csv to this directory: "/Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/", same location with Player.py. Commented Nov 28, 2018 at 4:00

3 Answers 3

1

Get the exact file path to the csv, if you are on a windows get the entire folder path and then the name, and then do:

with open(r'C:\users\path\players.csv') as csvfile: 

If you're using a windows and the exact path, easiest to put the r before the path like I did because it is a literal which will allow the string to be interpreted and the path to be found.

Sign up to request clarification or add additional context in comments.

Comments

0

You must put player.csv to the same location with your script Player.py

Example like your code, both files should be here: /Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/

Or you can put the specific directory of player.csv,

Ex:

with open("/Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/player.csv") as csvfile:
...

Comments

0

Check that you have the file in question in same directory as your .py file you're working on. If that doesn't work you might want to use the full path to the file:

with open ('/Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/player.CSV') as csvfile:

And you should try to check the name of the file too should be case sensitive otherwise, let's say you have Player.csv then don't try to open player.csv all lower case won't work!

Plus I don't know what you're trying to do with your CSV file, just print the raw content? You might like using pandas.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.