文件b'content / train.csv'不存在Google Colab

时间:2019-01-29 15:50:06

标签: google-colaboratory

首次使用Google Colab。我使用了Kaggle API,并且已将数据加载到Google Colab中,但似乎无法通过熊猫打开它。我右键单击该文件并复制路径。然后,我运行以下代码:

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

我得到的错误代码:

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

这是导致此错误的所有代码:

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

任何帮助都会很棒!

4 个答案:

答案 0 :(得分:2)

这也发生在我身上,但我能够通过使用新语法读取.csv文件来解决:

  • 在上方(第一或第二个)代码块中输入

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

然后执行此操作:

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

答案 1 :(得分:0)

我遇到了同样的问题,对我来说解决的是:

!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

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

答案 2 :(得分:0)

提供文件的完整路径,无论您身在何处。

尝试以下解决方案,希望它能在您从Google云端硬盘中获取数据的情况下起作用

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

答案 3 :(得分:0)

要在Google云端硬盘中打开文件,

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