我尝试从谷歌合作实验室下载小文件。它们很容易下载,但每当我尝试下载大尺寸的文件时,它会显示错误?大文件有什么办法下载?
如果您创建了一个大的 zip 文件,例如 my_archive.zip,那么您可以按如下方式下载它:
from google.colab import drive
drive.mount('/content/gdrive',force_remount=True)
!cp my_archive.zip '/content/gdrive/My Drive/downloads/'
!ls -lt '/content/gdrive/My Drive/downloads/'
最后,您可以将 zip 文件从 Google 驱动器下载到本地计算机。
这就是我处理这个问题的方法:
from google.colab import auth
from googleapiclient.http import MediaFileUpload
from googleapiclient.discovery import build
auth.authenticate_user()
然后点击链接,授权Google Drive并将代码粘贴到笔记本中。
drive_service = build('drive', 'v3')
def save_file_to_drive(name, path):
file_metadata = {
'name': name,
'mimeType': 'application/octet-stream'
}
media = MediaFileUpload(path,
mimetype='application/octet-stream',
resumable=True)
created = drive_service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print('File ID: {}'.format(created.get('id')))
return created
然后:
save_file_to_drive(destination_name, path_to_file)
这会将所有文件保存到您的 Google 云端硬盘,您可以在其中下载或同步它们或其他任何内容。
Google colab 不允许您使用
files.download()
下载大文件。但您可以使用以下方法之一来访问它:
如果您想从互联网直接下载一个大的 zip 文件到您的谷歌驱动器,这个解决方案适合我
!pip install gdown
import os
import gdown
import zipfile
# Define function to download file using gdown
def download_file_from_google_drive(id, destination):
url = f'https://drive.google.com/uc?id={id}'
gdown.download(url, destination, quiet=False)
# Define root directory for the dataset
destination_path = '/content/drive/MyDrive/Colab Notebooks/destination_path'
if not os.path.exists(destination_path):
os.makedirs(destination_path)
# Define file ID for the dataset
file_id = 'theFileId'
# Define destination path for the downloaded file
destination_path = os.path.join(destination_path, 'filename.zip')
# Download the file
download_file_from_google_drive(file_id, destination_path)