1

A basic question but I've got a file, say file.csv, in the directory, C:/Users/User/Desktop/Other/DataAnalysis/CurrentData/file.csv, and so I've written,

df = pd.read_csv("C:/Users/User/Desktop/Other/DataAnalysis/CurrentData/file.csv")

and I get an error -

> FileNotFoundError: File
> b'C:/Users/User/Desktop/Other/DataAnalysis/CurrentData/file.csv' does not exist

I've tried

df = pd.read_csv("file.csv")

As the file.csv is in the exact same folder as my python script, and I still get the same error. I'm not sure what I've done wrong. I've checked the python API and I've tried reversing the brackets (so using \\ instead of /), and still doesn't work. Thank you.

4
  • df = d.read_csv("C:/Users/User/Desktop/Other/DataAnalysis/CurrentData/file.csv") what is d here i guess you are referring to pandas as pd am I right? Commented Mar 13, 2018 at 8:48
  • Sorry, I didn't notice. Yes, it's suppose to be pd.read_csv. Let me edit that. Also, I do have "import pandas as pd ". Commented Mar 13, 2018 at 8:49
  • Is there a chance that your file full name is file.csv.csv and you hide extensions in your file system? @JamesWu Commented Mar 13, 2018 at 8:50
  • 1
    Hi Yoram, you're correct. It appears that I did have extensions hidden! Thanks for pointing that out. Commented Mar 13, 2018 at 8:52

3 Answers 3

2

Try something like:

with open(my_file, 'r') as f_in:
    for line in f_in:
        print(line)

That way you can check if Python can open the file at all.

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

2 Comments

Hi Uwain, I've used your code just then and it works perfectly. Based on Yoram's suggestion, I needed to add another .csv, but your code helps to see if it opens! I've got bits of code after and so it'll save me heaps of time! Cheers
Glad to help ;)
0

Check the extension it might be .csv.txt or .csv.csv

Comments

0

Window users

use forward slash:

import pandas as pd
df_swing = pd.read_csv('D:/anaconda/envs/data/EDA/2008_all_states.csv')

Example shown above is of absolute path. It's complete path from the base of your file system to the file that you want to load, e.g. c:/Documents/Shane/data/test_file.csv. Absolute paths will start with a drive specifier (c:/ or d:/ in Windows, or ‘/’ in Mac or Linux)

Comments

Your Answer

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