我有一个GoogleDrive文件夹的公共链接:https://drive.google.com/drive/folders/19RUYQNOzMJEA-IJ3EKKUf0qGyyOepzGk?usp=sharing
我想访问colab笔记本中的内容。 我希望打开笔记本的任何人都可以访问该文件夹,而不必安装我自己的驱动器。 Downloading public files in Google Drive (Python)之类的其他答案似乎建议对ID进行切片。 我尝试按照说明https://towardsdatascience.com/3-ways-to-load-csv-files-into-colab-7c14fcbdcb92
进行操作link= 'https://drive.google.com/drive/folders/19RUYQNOzMJEA-IJ3EKKUf0qGyyOepzGk?usp=sharing'
fluff, id = link.split('=')
print (id)
但是我的ID只是“共享”
编辑代码仍然无法正常工作
然后运行代码:
from google.colab import auth
auth.authenticate_user() # must authenticate
'''list all ids of files directly under folder folder_id'''
def folder_list(folder_id):
from googleapiclient.discovery import build
gdrive = build('drive', 'v3').files()
res = gdrive.list(q="'%s' in parents" % folder_id).execute()
return [f['id'] for f in res['files']]
'''download all files from a gdrive folder to current directory'''
def folder_download(folder_id):
for fid in folder_list(folder_id):
!gdown -q --id $fid
link='https://drive.google.com/drive/folders/1I6FwS5qB2bIwoPE4ueu8ZNH3upBqMB7S?usp=sharing'
folder_id="1I6FwS5qB2bIwoPE4ueu8ZNH3upBqMB7S"
folder_download(folder_id)
但出现此错误:
Permission denied: https://drive.google.com/uc?id=1AiNvRugUOWIthoSdBMBB5p5GLpyj6_Vd
Maybe you need to change permission over 'Anyone with the link'?
但是我已将权限更改为“具有链接的任何人”
编辑2:确保所有文件夹都具有可共享活动状态 在 Korakot Chaovavanich 评论之后,我确保每个文件/文件夹都是可共享的:
但是运行EDIT 1中提到的代码: 我收到此错误:
Permission denied: https://drive.google.com/uc?id=1AiNvRugUOWIthoSdBMBB5p5GLpyj6_Vd
Maybe you need to change permission over 'Anyone with the link'?
答案 0 :(得分:1)
您的folder_id在'/'和'?'之间。您可以使用split两次,也可以使用regexp提取它。
之后,您可能要列出其中的所有文件。这是gist的示例。关键部分是
'''list all ids of files directly under folder folder_id'''
def folder_list(folder_id):
from googleapiclient.discovery import build
gdrive = build('drive', 'v3').files()
res = gdrive.list(q="'%s' in parents" % folder_id).execute()
return [f['id'] for f in res['files']]
答案 1 :(得分:1)
我刚刚测试过的一种解决方案是将您的文件存储在Google云端硬盘以外的公共存储库中,然后使用!
调用shell命令并从那里检索文件。这是从公共Github存储库中将文件下载到Colab环境中的有效示例代码:
!wget https://raw.githubusercontent.com/heitorsf/pimpom/master/README.md
因此,您将在Colab上使用该文件。您可以使用!cat README.md
进行检查。
注意:执行此操作的最佳方法是使用文件“原始”版本的URL。
答案 2 :(得分:0)
1)。使用下面给出的代码后,您将在Google驱动器中获取目录列表,然后可以使用要使用的文件夹。
from google.colab import drive
drive.mount('/content/drive')
import os
os.listdir('/content/drive/My Drive')