首页 > 解决方案 > 如何修复 Colaboratory 中的 zipfile 读取错误?

问题描述

使用 colaboratory 将 zipfile 提取到我的谷歌驱动器时,我收到一条错误消息,提示 zipfile 读取错误。如何解决?

我正在尝试使用以下 python 3 脚本解压缩文件:

from google.colab import drive
drive.mount('/gdrive')

!unzip '/gdrive/My Drive/file.zip' -d '/gdrive/My Drive/Destination/'

从 zip 中提取 4 个文件后,出现此错误

error:  zipfile read error

标签: python-3.x

解决方案


尝试这个:

import zipfile
import os

file_location = 'file_path/file_name.zip'

with zipfile.ZipFile(file_location, 'r') as zip_ref:
    zip_ref.extractall('/content') # Replace '/content' with where you want to extract all files.

我希望它不会显示任何错误。


推荐阅读