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.
It is straight forward.
from google.colab import drive
drive.mount('/content/drive')
This will ask to open a url which will authorize the mount after you copy paste the token.
If you are not able to read files even now, then prefix your file path with this: 'drive/My Drive
' and you are good to go.
For example: file = 'drive/My Drive/data/file.txt'
Where data
is a directory in my Google Drive containing file.txt
file.
I ran into a similar issue last night. As some of the previous responders posted there are concerns that influence your ability to read the file. These concerns are, one, making certain that your file is accessible via google drive from your Collab notebook and also, two, making certain that your file is in the correct format.
I will explain the steps and include a screen shot.
from google.colab import drive
drive.mount('/content/drive')
Run the cell. You are prompted to accept permissions and get a token to use to mount the drive. Grant the permissions and copy and paste the code into the text input. Hit enter.
Below is the example sans Permission tab because I previously granted permissions.
I just tried mounting and creating a Drive file as you described and couldn't reproduce the error you describe.
https://colab.research.google.com/drive/17iiKJPQOPv1eW5-Ctf707mPHXDtipE5G
Perhaps try resetting your backend using the Runtime -> Reset all runtimes menu. Or, can you share a notebook illustrating the problem?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With