【发布时间】:2021-03-15 07:22:20
【问题描述】:
无法在 google colaboratory 中读取文件。 我在同一目录中有 .ipynb 文件和 .csv 文件,但是当我尝试运行时:
train = pd.read_csv("train.csv")
我明白了:
FileNotFoundError: File b'train.csv' does not exist
【问题讨论】:
无法在 google colaboratory 中读取文件。 我在同一目录中有 .ipynb 文件和 .csv 文件,但是当我尝试运行时:
train = pd.read_csv("train.csv")
我明白了:
FileNotFoundError: File b'train.csv' does not exist
【问题讨论】:
我希望您在打开 train 文件之前已经运行了此代码。
# 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
# Generate auth tokens for Colab
from google.colab import auth
auth.authenticate_user()
# 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}
# Create a directory and mount Google Drive using that directory.
!mkdir -p drive
!google-drive-ocamlfuse drive
print ('Files in Drive:')
!ls drive/
执行上述代码后,只需在google-colaboratory 中打开您的train 文件
train = pd.read_csv('drive/...{folder_name}.../train.csv, encoding='utf8')
我希望这会有所帮助!
【讨论】:
我使用的是 Windows 10,这对我来说非常有效。试试看吧。
将新文件夹添加到您的驱动器中。把它命名为你想要的。在我的例子中,我将它命名为 “Colab Notebook”。这是我保存代码和数据文件的文件夹。
首先,您需要安装驱动器。为此,请一一运行以下内容
from google.colab import drive
drive.mount('/content/drive/')
在第二个命令之后,它会弹出身份验证密钥所在的链接。打开此链接复制密钥,粘贴并按回车键。
现在输入!ls 它必须给出类似drive sample_data的内容
上传您的数据文件。无论是 csv 文件还是 excel 文件都无所谓,但每个文件的命令会有所不同。
对于 csv 文件
train = pd.read_csv('/content/drive/My Drive/Colab Notebook/train.csv')
excel文件也一样,只是改变pandas命令和文件扩展名
【讨论】:
Mounted at /content/drive/我的意思是当你复制和粘贴验证码时。
!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
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
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')
【讨论】:
downloaded = drive.CreateFile({'id':'1BH-rffqv_1auzO7tdubfaOwXzf278vJK'}) # 将id替换为你要访问的文件的id downloaded.GetContentFile('xyz.csv') #3.将文件读取为熊猫数据框import pandas as pdxyz = pd.read_csv('xyz.csv')
确保在路径的开头放置 /。 通过从文件资源管理器中右键单击已安装的驱动器来复制路径,它不会复制开始的正斜杠。确保将其添加到路径中。
【讨论】: