如何使用 Google colab 将文件下载到 Google Drive?

M.G*_*eel 3 python downloadfile google-colaboratory

我一直在使用此代码通过 Google Drive 安装 Colab 并通过粘贴下载 URL 来下载任何文件,但我注意到即使文件大小只有几兆字节也需要很长时间。有什么可以做的来改善它。

**First cell:**
from google.colab import drive
drive.mount('/content/gdrive')
root_path = 'gdrive/My Drive/' 

**Second cell:**
import requests  
file_url = "DOWNLOAD URL HERE"

r = requests.get(file_url, stream = True)  

with open("/content/gdrive/My Drive/FILE NAME HERE", "wb") as file:  
    for block in r.iter_content(chunk_size = 1024): 
         if block:  
             file.write(block)
Run Code Online (Sandbox Code Playgroud)

小智 9

我更喜欢使用!wget方法。

!wget "Specify URL you want to download" -P "specify DIRECTORY where you want contents of URL"

例如。

!wget "https://github.com/jbrownlee/Datasets/releases/download/Flickr8k/Flickr8k_Dataset.zip" -P "/content/drive/My Drive/imgcaptiongen/data"
Run Code Online (Sandbox Code Playgroud)

这种方式要容易得多。