Can't read a file in google colaboratory

感情迁移 提交于 2019-12-04 08:36:48

问题


Can't read a file in google colaboratory . I have .ipynb file and .csv files in the same directory but when I try to run:

train = pd.read_csv("train.csv") 

I get:

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


回答1:


I hope you have run this code before opening your train file.

Part I

# Install a Drive FUSE wrapper. # https://github.com/astrada/google-drive-ocamlfuse !apt-get install -y -qq software-properties-common python-software-properties module-init-tools !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null !apt-get update -qq 2>&1 > /dev/null !apt-get -y install -qq google-drive-ocamlfuse fuse

Part II

# Generate auth tokens for Colab

from google.colab import auth auth.authenticate_user()

Part III

# Generate creds for the Drive FUSE library.

from oauth2client.client import GoogleCredentials creds = GoogleCredentials.get_application_default() import getpass !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL vcode = getpass.getpass() !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

Part IV

# Create a directory and mount Google Drive using that directory.

!mkdir -p drive !google-drive-ocamlfuse drive

Part V

print ('Files in Drive:') !ls drive/

After implementing the above codes just open your train file in google-colaboratory

train = pd.read_csv('drive/...{folder_name}.../train.csv, encoding='utf8')

I hope this helps!




回答2:


Install the PyDrive wrapper & import libraries.

This only needs to be done once per notebook.

!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.

This only needs to be done once per notebook.

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

Download a file based on its file ID.

A file ID looks like: laggVyWshwcyP6kEI-y_W3P8D26sz

file_id = 'REPLACE_WITH_YOUR_FILE_ID'
downloaded = drive.CreateFile({'id': file_id})
downloaded = drive.CreateFile({'id':'1BH-rffqv_1auzO7tdubfaOwXzf278vJK'}) # replace the id with id of file you want to access
downloaded.GetContentFile('xyz.csv')  

# Read file as panda dataframe
import pandas as pd
xyz = pd.read_csv('xyz.csv') 



回答3:


I use windows 10 and this worked perfectly for me. Give it try.

Add new folder into your drive. Name it what you want. In my case I named it "Colab Notebook". This is folder where I keep my codes and data file.

First you need to mount your drive. For this run the following one by one

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

After second command it pops link where the authentication key is. Open this link copy the key, paste and press enter.

Now type !ls it has to give something like this drive sample_data

Upload your data file. Either it will be csv or excel file does not matter, but commands will be different for each.

For csv file

train = pd.read_csv('/content/drive/My Drive/Colab Notebook/train.csv')

For excel file it's the same just change pandas command and file extension




回答4:


make sure you put a / in the beginning of the path. Whey you copy path by right click on your mounted drive from the file explorer, it does not copy the beginning forward slash. make sure you add it in the path.



来源:https://stackoverflow.com/questions/48967757/cant-read-a-file-in-google-colaboratory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!