rmahesh
rmahesh

Reputation: 749

File b'content/train.csv' does not exist Google Colab

First time using Google Colab. I have used a Kaggle API and I have the data loaded into Google Colab, but I can't seem to open it via pandas. I right clicked on the file and copied path. I then ran the following code:

import pandas as pd
train = pd.read_csv("content/train.csv") 
test = pd.read_csv('content/test.csv')

The error code that I am getting:

FileNotFoundError: File b'content/train.csv' does not exist

Here is the code for everything I have done leading up to this error:

!pip install kaggle
from google.colab import files
files.upload() #Uploaded my kaggle.json file

!pip install -q kaggle
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/

!kaggle competitions download -c microsoft-malware-prediction

#Unzip the files:
!7z x train.csv.zip
!7z x sample_submission.csv.zip
!7z x test.csv.zip

#remove the zipped data
!rm train.csv.zip
!rm sample_submission.csv.zip
!rm test.csv.zip

import pandas as pd
train = pd.read_csv("content/train.csv") 
test = pd.read_csv('content/test.csv') 
print('read')

Any help would be great!

Upvotes: 2

Views: 5181

Answers (4)

codeswithroh
codeswithroh

Reputation: 25

In order to open a file in Google Drive:

df=pd.read_csv(" copy and paste the entire path of that file")

Upvotes: 0

saurabh kumar
saurabh kumar

Reputation: 1

Provide the full path for the file irrespective of where you are.

Try below solution, hope it works if you are fetching data from google drive

data = pd.read_csv("/content/drive/My Drive/data/"name_of_the_csv_file")

Upvotes: 0

Omer Anisfeld
Omer Anisfeld

Reputation: 1302

i encountered with the same issue , what fixed it for me was :

!pip install -U -q PyDrive

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# PyDrive reference:
# https://gsuitedevs.github.io/PyDrive/docs/build/html/index.html

# 2. Create & upload a file text file.
uploaded = drive.CreateFile({'title': 'Sample upload.txt'})
uploaded.SetContentString('Sample upload file content')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))

# 3. Load a file by ID and print its contents.
downloaded = drive.CreateFile({'id': uploaded.get('id')})

print('Downloaded content "{}"'.format(downloaded.GetContentString()))
    from google.colab import drive
    drive.mount('/content/gdrive', force_remount=True)
    root_dir = "/content/gdrive/My Drive/"
    base_dir = root_dir + 'app/'
    and then each file is refreed as base_dir +file_name

from https://colab.research.google.com/notebooks/io.ipynb#scrollTo=zU5b6dlRwUQk

Upvotes: 0

Sogo Ogundowole
Sogo Ogundowole

Reputation: 46

It happened to me too but I was able to resolve with reading the .csv file with a new syntax:

  • Enter this in a code block above (1st or second)

!pip install -U -q PyDrive

from pydrive.auth import GoogleAuth

from pydrive.drive import GoogleDrive

from google.colab import auth from oauth2client.client import GoogleCredentials

#Authenticate and create the PyDrive client

auth.authenticate_user()

gauth = GoogleAuth()

gauth.credentials = GoogleCredentials.get_application_default()

drive = GoogleDrive(gauth)

Then do this:

link = 'link_to_file_in drive'

fluff, id = link.split('=')

downloaded = drive.CreateFile({'id':id})

downloaded.GetContentFile('name_of_file.csv')

df = pd.read_csv("name_of_file.csv")

Upvotes: 3

Related Questions