首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python - ZipFile‘对象没有'seek’属性

Python - ZipFile‘对象没有'seek’属性
EN

Stack Overflow用户
提问于 2018-01-10 05:43:41
回答 2查看 11.5K关注 0票数 0

我这里的代码有问题。我正在尝试获得一个脚本,可以制作一个ePub文件。它们是压缩的zip文件,经过压缩(即未压缩),必须按顺序执行。此当前脚本将创建一个.zip,但在运行zip -t命令时,它在Python Shell和终端应用程序中都无法使用和创建错误。

在Python shell中,有问题的错误如下:

代码语言:javascript
复制
Traceback (most recent call last):
  File "/Users/Hal/Documents/GitHub/Damore-essay-ebook/GenEpub-old.py", line 29, in <module>
    if zipfile.is_zipfile(zf) is True:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/zipfile.py", line 183, in is_zipfile
    result = _check_zipfile(fp=filename)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/zipfile.py", line 169, in _check_zipfile
    if _EndRecData(fp):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/zipfile.py", line 241, in _EndRecData
    fpin.seek(0, 2)
AttributeError: 'ZipFile' object has no attribute 'seek'

Mac终端上有问题的错误(尽管我确信无论我在哪里运行zip -t,输出都是一样的

代码语言:javascript
复制
Archive:  IdealogicalEcho.epub
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of IdealogicalEcho.epub or
        IdealogicalEcho.epub.zip, and cannot find IdealogicalEcho.epub.ZIP, period.

Python源码:

代码语言:javascript
复制
#!/usr/bin/env python

#GenEpub.py - Generates an .epub file from the data provided.
#Ideally with no errors or warnings from epubcheck (needs to be implemented, maybe with the Python wrapper).

import os
import json
import zipfile

with open('metadata.json') as json_file:
        data = json.load(json_file)

#The ePub standard requires deflated compression and a compression order.
zf = zipfile.ZipFile(data["fileName"] + '.epub', mode='w', compression=zipfile.ZIP_STORED)

zf.write(data["fileName"] + '/mimetype')

for dirname, subdirs, files in os.walk(data["fileName"] + '/META-INF'):
    zf.write(dirname)
    for filename in files:
        zf.write(os.path.join(dirname, filename))

for dirname, subdirs, files in os.walk(data["fileName"] + '/EBOOK'):
    zf.write(dirname)
    for filename in files:
        zf.write(os.path.join(dirname, filename))

#zipfile has a built-in validator for debugging
if zipfile.is_zipfile(zf) is True:
    print("ZIP file is valid.")

#Extra debugging information
#print(getinfo.compress_type(zf))
#print(getinfo.compress_size(zf))
#print(getinfo.file_size(zf))

zf.close()

我使用的JSON文件:

代码语言:javascript
复制
{
        "comment1": "Metadata.json - Insert the e-book's metadata here. WIP",

        "comment2": "Technical metadata - This is the where the cover image is specified. Recommended to use ePub V2.0.1 over 3.0 for epubVersion and Reflowable rather than Fixed for textPresentation (unless doing a project that requires a specific layout). mobiCover and generateKindle are currently unused but added for futureproofing.",
        "epubCover": "cover.jpg",
        "mobiCover": "cover.jpg",
        "fileName": "IdealogicalEcho",
        "epubVersion": "2.0.1",
        "textPresentation": "Reflowable",
        "generateKindle": "no",

        "comment3": "Book metadata - Information about the e-book itself. Language is specified with ISO 639-1. Rights can be worldwide, country specific or under a permissable license such as Creative-Commons SA",
        "title": "Google's Idealogical Echochamber",
        "creator": "James Damore",
        "subject": "Academic",
        "publisher": "Hal Motley",
        "ISBN": "-",
        "language": "en",
        "rights": "Creative-Commons SA",

        "comment4": "This is the page order that the e-book has. The first number before the colon is the page order, the second is the indentation, third is the page name and fourth is file itself.",
            "pages": [
                    {
                        "1": [0, "Cover", "bookcover.xhtml"],
                        "2": [0, "Title", "title.xhtml"],
                        "3": [0, "Indicia", "indicia.xhtml"],
                        "4": [0, "License", "license.xhtml"],
                        "5": [0, "Contents", "toc.xhtml"],
                        "6": [0, "Foreword", "foreword.xhtml"],
                        "7": [0, "Article", "article.xhtml"]
                    }
                            ]
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-10 07:57:30

问题出在is_zipfile内部的某个地方。尽管保留了“文件名可能是一个文件或类似文件的对象”(13.5.1. ZipFile Objects: zipfile.is_zipfile),但它失败了,并出现了seek错误。

一种可能的解决方案是关闭该文件,然后重新打开它以进行检查:

代码语言:javascript
复制
zf.close()

with open(data["fileName"] + '.epub','r') as f:
    if zipfile.is_zipfile(f) is True:
        print("ZIP file is valid.")

我还发现,这种检查是非常基本的,即使您手动损坏了一些字节,它也会返回True。它需要一些努力才能真正使其失败。

有趣的是,明显更彻底的zipfile.ZipFile.testzip函数再次需要该zf -但如果在zf.close()之前调用,它也会失败。也没有zf.flush() ..。

幸运的是,在运行脚本后使用zip检查创建的ePub文件会发现它不包含任何错误:

代码语言:javascript
复制
~/Documents $ zip -T IdealogicalEcho.epub 
test of IdealogicalEcho.epub OK

(顺便说一句,它不会告诉您它是一个有效的epub。(事实并非如此。)

票数 1
EN

Stack Overflow用户

发布于 2018-01-10 05:48:40

我建议您在验证之前尝试关闭。对仍处于打开状态以进行写入的文件执行整个文件操作可能不会产生有效的结果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48177085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档