【问题标题】:Python: Writing a code that recognizes an object and counts itPython:编写识别对象并对其进行计数的代码
【发布时间】:2019-09-03 20:45:10
【问题描述】:

我正在编写一个python程序,使用colab计算和识别图像中的对象,当我在以下代码中遇到错误时,

drive_url = 'https://drive.google.com/uc?export=download&confirm=jH_3&id=' + DATASET_DRIVEID
file_name = DATASET_DRIVEID + '.zip'

urllib.request.urlretrieve(drive_url, file_name)
print('Download completed!')

os.makedirs(DATASET_DIR, exist_ok=True)
with zipfile.ZipFile(file_name, 'r') as zip_ref:
  zip_ref.extractall(DATASET_DIR)
os.remove(file_name)
print('Extract completed!')

文件下载已成功完成,但我认为提取部分中的错误显示为以下错误:

Download completed!
---------------------------------------------------------------------------
BadZipFile                                Traceback (most recent call last)
<ipython-input-5-6646b94bdb6f> in <module>()
      6 
      7 os.makedirs(DATASET_DIR, exist_ok=True)
----> 8 with zipfile.ZipFile(file_name, 'r') as zip_ref:
      9   zip_ref.extractall(DATASET_DIR)
     10 os.remove(file_name)

/usr/lib/python3.6/zipfile.py in __init__(self, file, mode, compression, allowZip64)
   1129         try:
   1130             if mode == 'r':
-> 1131                 self._RealGetContents()
   1132             elif mode in ('w', 'x'):
   1133                 # set the modified flag so central directory gets written

/usr/lib/python3.6/zipfile.py in _RealGetContents(self)
   1196             raise BadZipFile("File is not a zip file")
   1197         if not endrec:
-> 1198             raise BadZipFile("File is not a zip file")
   1199         if self.debug > 1:
   1200             print(endrec)

BadZipFile: File is not a zip file

有什么建议吗?

【问题讨论】:

    标签: python keras google-colaboratory


    【解决方案1】:

    您的 Python 代码看起来正确,但 drive_url 的值除外。

    错误消息表明名为file_name 的文件不是ZIP 文件。 urlretrieve 可能下载了其他内容,例如错误页面或不同格式的文件。可能您的drive_url 不正确(即不是您的浏览器使用的下载链接,而是网页链接),或者上传者同时从 Google Drive 中删除了该文件,或者您没有指定必要的 cookie下载。

    使用您的网络浏览器下载drive_url,并查看下载的文件(例如,在Linux 上使用file 命令)。

    【讨论】:

    • 该网址运行良好,但我想问题是当我手动尝试下载该网址时,它会显示网络可能有一些病毒的窗口@pts
    • 与其猜测,不如看看下载的文件(我的回答中建议)?
    • 您需要在 Python 脚本中使用与浏览器相同的下载链接。如果您使用 Chrome,您可以在 chrome://downloads/ 页面上找到最新的下载链接。您可能还需要设置 cookie:Python 脚本应该使用与您的浏览器使用的相同的 cookie——您可以提出一个单独的问题,了解如何将 cookie 从浏览器复制到 urllib.request.urlretrieve
    猜你喜欢
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    相关资源
    最近更新 更多