-1

hey guys i am new to python and have been trying to use google collaboratory notebook to learn pandas. i have been trying to import data but i was unable to do so, the error being :

`FileNotFoundError: [Errno 2] No such file or directory: './train.csv'`

but i had the csv file in my folder which my notebook is in.

This is the code i used to run. i had no idea why it doesnt work. Thanks for any suggestions.

train = pd.read_csv("./train.csv")
test = pd.read_csv("./test.csv")
Joel2342
  • 23
  • 2
  • 5
  • 1
    The two comments that suggest using `"train.csv"` rather than `"./train.csv"` are idiotic since the two paths are identical; even on MS Windows. It's pretty clear from the error that the CWD (current working directory) is not what you expect. If you replace the relative path with an absolute path does it work? I expect it will; or at least fail for a different reason other than not finding the file. In which case the question becomes "Why isn't the CWD what I expect?" – Kurtis Rader Nov 05 '20 at 05:07
  • This issue might be relevant: https://stackoverflow.com/questions/47320052/load-local-data-files-to-colaboratory – Kurtis Rader Nov 05 '20 at 05:22

1 Answers1

-1

Assuming you uploaded your files in Google colab correctly, I suspect that you're not using the exact location of the files (test.csv and train.csv)

Once you navigate to the location of the files, find the location using

pwd

Once you find the location, you can read the files in pandas

train = pd.read_csv(Location_to_file)
test = pd.read_csv(location_to_file)
vasu
  • 173
  • 1
  • 11
  • Let us know the location of the files and if this resolved your query – vasu Nov 05 '20 at 05:10
  • They are "using the exact location of the files." They simply expect the CWD to be something other than the actual current working directory. – Kurtis Rader Nov 05 '20 at 05:11
  • If that's the case use absolute path – vasu Nov 05 '20 at 05:15
  • I disagree with your comment. Since, the user of the question mentions that `csv file in my folder which my notebook is in.`, which mean that the files should be in CWD. My answer just aims to find exactly where their files are: are they in CWD or are they somewhere else. – vasu Nov 05 '20 at 05:30
  • You overlooked that they are using Google Colab. See https://colab.research.google.com/notebooks/io.ipynb. Their files are "somewhere else" with respect to Google Colab. – Kurtis Rader Nov 05 '20 at 05:35
  • No I didn't. Since they mention their files are in the same directory as of the notebook, which means they would've uploaded the files in google colab. However, I edited my answer to ensure they have uploaded the files in Colab. – vasu Nov 05 '20 at 05:38
  • You failed to explain why what they did failed. If "their files" are in the same directory as the notebook why did the attempt to read those files using a relative path fail? Is it expected that the CWD will be changed when uploading and executing a notebook? What is the best practice for dealing with that situation? – Kurtis Rader Nov 05 '20 at 05:49