从谷歌colab中的驱动器读取文件 [英] Read file from drive in google colab
问题描述
我已阅读有关如何打开驱动器的笔记本.我已经按照指示使用:
I Have read the notebook about how to open drive. I already did as instructed using:
from google.colab import drive
drive.mount('/content/drive')
之后,我可以使用 !ls
列出驱动器的内容,但无法读取或打开任何文件.我已经试过了:
After this, I can use !ls
to list the contents of my drive but I cannot read or open any file. I already tried:
with open("/content/drive/My Drive/filename.ext", "r") as file:
file = open("/content/drive/My Drive/filename.ext", "r")
!cp "/content/drive/My Drive/filename.ext" "filename.ext"
还有
import pandas as pd
file = pd.read_csv("/content/drive/My Drive/filename.ext")
但以上都没有奏效.我总是得到不支持操作"或无法打开文件进行阅读".我看到了一些使用 PyDrive 的建议.但这是通过将文件从 Google Drive 复制到 Google Drive 来完成的.我不明白为什么你必须来回复制文件,因为我需要遍历文件夹中的所有文件.为什么 google colab 不能只读取存储在驱动器上的文件?还是我做错了什么?另一件事是我上传了一堆 csv 文件,但谷歌驱动器将它们列为.csv.gsheet"(使用 glob).这可能是问题吗?我没有其他想法.
But none of the above worked. I always get "operation not supported" or "cannot open file for reading". I have seen some suggestin to use PyDrive. But it is done by copy file from Google Drive to Google Drive. I don't get why you would have to copy back and forth files, since I need to iterate over all the files on the folder. Why can't google colab just read the file stored on drive? Or am I doing something wrong? Another thing is that I uploaded a bunch of csv files, but google drive lists them as ".csv.gsheet" (using glob). Could that be the problem? I have no other ideas.
推荐答案
我(部分)根据 Bob Smith 和 Ami F 的回答找出了发生的事情.
我相信谷歌驱动器会阻止从转换为驱动器格式(gsheet、gdoc 等)的文件中读取访问权限.因此,每当我尝试使用 !cat
或 open
时,都会出现操作不受支持"错误.当我尝试 Bob 的示例,创建一个文件然后读取它时,它在我的笔记本上运行.
所以我设法阻止谷歌转换文件,删除旧文件并上传所有内容以再次驱动.现在,我所有的 csv 都保持不变(没有 .gsheet 扩展),我可以使用 open 访问它们.
I (partially) found out what was going on based on Bob Smith and Ami F's answers.
I believe google drive blocks read access from files converted to drive formats (gsheet, gdoc, etc.). And so, whenever I tried to use !cat
or open
, I got an "operation unsupported" error. When I tried Bob's example, creating a file and then reading it, it worked on my notebook.
So I managed to prevent google from converting files, deleted the old files and uploaded everything to drive again. Now, all my csv's were being kept unchanged (no .gsheet extesion) and I am able to access them using open.
这篇关于从谷歌colab中的驱动器读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!