lavender
lavender

Reputation: 399

FileNotFoundError: [Errno 2] No such file or directory google colab

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')

Upvotes: 28

Views: 204409

Answers (8)

Dismas
Dismas

Reputation: 577

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

Upvotes: 1

to run a python file in the Google Drive;

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

Upvotes: 0

Jean Fernandez
Jean Fernandez

Reputation: 61

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

Upvotes: 1

K-Beadle
K-Beadle

Reputation: 11

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*/')

Upvotes: 0

chamod rathnayake
chamod rathnayake

Reputation: 981

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

Upvotes: 1

Ankeit Taksh
Ankeit Taksh

Reputation: 166

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

Upvotes: 2

Peyman
Peyman

Reputation: 4179

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.

Upvotes: 8

Rishabh Gupta
Rishabh Gupta

Reputation: 434

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

Upvotes: 29

Related Questions