Dav*_*d 8 6 python url download drive google-colaboratory
我有一个 GoogleDrive 文件夹的公共链接:https ://drive.google.com/drive/folders/19RUYQNOzMJEA-IJ3EKKUf0qGyyOepzGk ? usp = sharing
我想访问 colab 笔记本中的内容。 我希望打开笔记本的任何人都能够访问该文件夹,因此无需安装我自己的驱动器。在 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)
Run Code Online (Sandbox Code Playgroud)
但是我的 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)
Run Code Online (Sandbox Code Playgroud)
但得到这个错误:
Permission denied: https://drive.google.com/uc?id=1AiNvRugUOWIthoSdBMBB5p5GLpyj6_Vd
Maybe you need to change permission over 'Anyone with the link'?
Run Code Online (Sandbox Code Playgroud)
但是我已将权限更改为“任何有链接的人”
编辑 2:确保所有文件夹都处于可共享状态 在 Korakot Chaovavanich评论之后,我确保每个文件/文件夹都是可共享的:
但是运行编辑 1 中提到的代码:我收到此错误:
Permission denied: https://drive.google.com/uc?id=1AiNvRugUOWIthoSdBMBB5p5GLpyj6_Vd
Maybe you need to change permission over 'Anyone with the link'?
Run Code Online (Sandbox Code Playgroud)
您的folder_id 位于“/”和“?”之间。您可以使用 split 两次或使用 regexp 来提取它。
之后,您可能想要列出其中的所有文件。这是要点示例。关键部分是
'''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']]
Run Code Online (Sandbox Code Playgroud)