25

I am getting this error everytime running python code on local data from my drive I am using the code below to import the data from my drive

from google.colab import drive
drive.mount('/content/drive')
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
lavender
  • 369
  • 1
  • 3
  • 9

8 Answers8

28

Check if your file exists in this "/content/drive/" location

import os
os.chdir("/content/drive/")
!ls

Most likely it would be under 'My Drive'

import os
os.chdir("/content/drive/My Drive")
!ls
Rishabh Gupta
  • 424
  • 4
  • 8
  • 1
    This also resolves issue of FileNotFound: "/content/drive/My" because of the space between My and Drive. – chikitin Nov 24 '19 at 13:36
  • doesn't work for me either, I simply get: FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/'. Are you sure it meant to work under Windows? – Eli Borodach Jul 25 '21 at 12:52
  • Eli, it can work under windows when you use regex. for example, it can be os.chdir(r"/cotent/drive/My Drive"). this threw away the error raised by the space between My and Drive. – Dismas Apr 12 '22 at 14:52
7

First, check the path. Make sure the file exists in the right path.

How to make that sure?

Simple. Just use terminal code like this example of mine:

! ls /content/drive/My\ Drive/data/sahamyab

colabs respons: data.jl sahamyab.zip tweets.json

If your file is not on that path, then you are in the wrong way. Otherwise, after you see that the file exists, just open it like this:

open("/content/drive/My Drive/data/sahamyab/tweets.json")

WARNING: look at paths carefully. The first one has My\ Drive and the second My Drive. because the first one is on the terminal path and space is \ but the second one is in the python path.

Community
  • 1
  • 1
Peyman
  • 3,097
  • 5
  • 33
  • 56
2

google has changed to Mydrive and deleted space. so no "My Drive" but "MyDrive os.chdir('/content/gdrive/MyDrive/iss/vse/data/')

1
os.chdir('/content/drive/MyDrive')
ls

to check whether the file exists

try to avoid spaces when you are creating new directories inside the drive cos it can lead to compile-time errors

If you just want to mount your drive to the colab you can do that without any code using the web IDE

enter image description here

1

For troubleshooting above and to find and load local data files in Google Colab:

  1. Upload data file from your system memory to Google drive:
  2. Mount Google drive in Colab: 2.1) Import at your code: from google.colab import drive 2.2) mount the directory where is the data at google drive: drive.mount('/content/gdrive') 2.3) To mount the directory, it will be required authorization for your google account
  3. path = "/gdrive/My Drive/filename"

Reference: Load local data files to Colaboratory

1

I came over the issue when I used regex as follows. You may also want to try it.

import os
os.chdir(r"/content/drive/My Drive")

Note the position of r, for regex

Dismas
  • 377
  • 1
  • 3
  • 10
0

to run a python file in the Google Drive;

!python3 /content/drive/My\ Drive/data/file.py
-1

I had the same issue, for me it was that "drive" needs to be spelled with an r as "driver". "My Drive" including the space worked for me as well.

os.chdir('/content/driver/My Drive/*filename*/')
Skulaurun Mrusal
  • 2,792
  • 1
  • 15
  • 29