0

I have my iris.txt stored at my Google Drive at My Drive/ML, I was trying to open and read it.

import csv
import numpy

file = open(r"/drive/ML/iris.txt")
mylist = list(csv.reader(file))
lines = numpy.asarray(mylist)

It returned me the error, how should I do?

IOErrorTraceback (most recent call last)
<ipython-input-27-5be95fc15074> in <module>()
      6 from sklearn.cluster import KMeans
      7 
----> 8 file = open(r"/my drive/ML/iris.txt")
      9 mylist = list(csv.reader(file))
     10 lines = numpy.asarray(mylist)

IOError: [Errno 2] No such file or directory: '/my drive/ML/iris.txt'
John
  • 75
  • 1
  • 7
  • 1
    You should retrieve the file from Google drive to a local folder where you could read it from. Check [Google doc for Drive](https://developers.google.com/api-client-library/python/apis/drive/v2) – Cleptus Sep 10 '18 at 07:51
  • I uploaded it from a local folder – John Sep 10 '18 at 08:08
  • Possible duplicate of [Google Colab: how to read data from my google drive?](https://stackoverflow.com/questions/48376580/google-colab-how-to-read-data-from-my-google-drive) – scraaappy Sep 10 '18 at 20:48

2 Answers2

0

Use this:

from `google.colab` import files

uploaded = files.upload()

import io

df2 = pd.read_csv(io.BytesIO(uploaded['ex1data1.csv']))
Andrew Regan
  • 4,887
  • 6
  • 35
  • 67
0
from google.colab import files
uploaded = files.upload()

for f in uploaded.keys():
    file = open(f, 'r')
    lines = file.readlines()
  • While this code may answer the question, providing additional context regarding _why_ and/or _how_ this code answers the question improves its long-term value. – Swier Sep 23 '20 at 17:16