Search code examples
google-drive-apigoogle-colaboratory

Read file from drive in google colab


I Have read the notebook about how to open drive. I already did as instructed using:

from google.colab import drive
drive.mount('/content/drive')

After this, I can use !ls to list the contents of my drive but I cannot read or open any file. I already tried:

with open("/content/drive/My Drive/filename.ext", "r") as file:

file = open("/content/drive/My Drive/filename.ext", "r")

!cp "/content/drive/My Drive/filename.ext" "filename.ext"

and also

import pandas as pd

file = pd.read_csv("/content/drive/My Drive/filename.ext")

But none of the above worked. I always get "operation not supported" or "cannot open file for reading". I have seen some suggestin to use PyDrive. But it is done by copy file from Google Drive to Google Drive. I don't get why you would have to copy back and forth files, since I need to iterate over all the files on the folder. Why can't google colab just read the file stored on drive? Or am I doing something wrong? Another thing is that I uploaded a bunch of csv files, but google drive lists them as ".csv.gsheet" (using glob). Could that be the problem? I have no other ideas.


Solution

  • I (partially) found out what was going on based on Bob Smith and Ami F's answers.
    I believe google drive blocks read access from files converted to drive formats (gsheet, gdoc, etc.). And so, whenever I tried to use !cat or open, I got an "operation unsupported" error. When I tried Bob's example, creating a file and then reading it, it worked on my notebook.
    So I managed to prevent google from converting files, deleted the old files and uploaded everything to drive again. Now, all my csv's were being kept unchanged (no .gsheet extesion) and I am able to access them using open.