簡體   English   中英

如何使用 Google colab 將文件下載到 Google Drive?

[英]How to download files to Google Drive using Google colab?

我一直在使用此代碼通過 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)

我更喜歡使用!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"

這種方式要容易得多。

不要將文件下載到 Google Drive,因為在 Colab 中訪問 Google Drive 會產生開銷,尤其是在其中寫入文件。

如果此文件是臨時文件,只需將其下載到/tmp (或使用tempfile.gettempdir使代碼更漂亮)。 如果它不是臨時的,仍然考慮將其下載到臨時文件夾,然后在下載結束時將其復制到雲端硬盤(同時繼續使用本地副本以提高速度)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM