1

I'm new to python and trying to learn a few exercises via colab. I want to import a CSV file that I saved to my desktop. Unfortunately, I keep getting a "cannot find file" error message. Not sure what I'm doing wrong.

Here's my code:

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

mpg = pd.read_csv(r"C:\Users\micha\OneDrive\Desktop\mpg2018.csv.csv")

I tried to change csv.csv to csv.txt or leave as just .csv but nothing works. Any help would be great!

AMC
  • 2,466
  • 7
  • 11
  • 31
mh2019
  • 11
  • 1
  • Is your file really name `mpg2018.csv.csv` or is it `mpg2018.csv`? – Nathan Wride Apr 27 '20 at 23:40
  • Also you should try using forward slashes in your path. e.g `mpg = pd.read_csv("C:/Users/micha/OneDrive/Desktop/mpg2018.csv")` – Nathan Wride Apr 27 '20 at 23:44
  • I am not sure you can read files from OneDrive like that. OneDrive is being used as cloud storage in this case? – griffin_cosgrove Apr 27 '20 at 23:51
  • Does this answer your question? [Import data into Google Colaboratory](https://stackoverflow.com/questions/46986398/import-data-into-google-colaboratory) – AMC Apr 28 '20 at 00:39
  • _trying to learn a few exercises via colab_ Colab cannot directly access your local files like this. Take a look at https://colab.research.google.com/notebooks/io.ipynb. – AMC Apr 28 '20 at 00:40

3 Answers3

0

Foward slashes will work in this function.

mpg = pd.read_csv("C:/Users/micha/OneDrive/Desktop/filename.csv")

Since you've imported "os", you could also use path.join()

p = os.path.join("C:\\", "Users", "micha", "OneDrive", "Desktop", "a.csv")
mpg = pd.read_csv(p)

Also, that file format repetition within the name seems unnecessary. It may lead to more confusion.

Fell
  • 13
  • 1
  • 3
  • How do you know that is the issue? _Since you've imported "os", you could also use path.join()_ Or even better, they could use pathlib ;) – AMC Apr 28 '20 at 00:41
0

You are using colab.research.google.com, which lives in its own cloud world and has no idea of what files are on your personal machine. However, if you

from google.colab import files
files.upload()

It will open a nice dialog box which will allow you to find the file in the usual way.

Igor Rivin
  • 3,463
  • 1
  • 15
  • 19
0

In files section you find Upload button click on it add your file in colab now simply import pd.read_csv(path)

how you find path: left click on csv file then select copy path and paste in place of path in pd.read_csv()

Jai Mahesh
  • 1,080
  • 8
  • 17