【问题标题】:File b'content/train.csv' does not exist Google Colab文件 b'content/train.csv' 不存在 Google Colab
【发布时间】:2019-01-29 15:50:06
【问题描述】:

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

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

任何帮助都会很棒!

【问题讨论】:

  • 您的路径中是否缺少领先的/?即/content/... 而不是content/...
  • 你好。是的,我已经包括在内了。我现在怀疑这是我获取数据的来源的权限问题。不过感谢您的帮助。

标签: google-colaboratory


【解决方案1】:

这也发生在我身上,但我能够通过使用新语法读取 .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")

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,为我解决的是:

    !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

    【讨论】:

      【解决方案3】:

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

      尝试以下解决方案,如果您从谷歌驱动器获取数据,希望它可以工作

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

      【讨论】:

        【解决方案4】:

        为了在 Google Drive 中打开文件:

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

        【讨论】: