Zipfiles的Python问题 [英] Python issue with Zipfiles

查看:106
本文介绍了Zipfiles的Python问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:编码新手

C:\Python27\python.exe C:/Users/Evolution/PycharmProjects/MCMODVERSION/stuff/main.py
<open file 'C:\\Users\\Evolution\\Desktop\\mods\\AT_StalkerCreepers_1.6.4.jar', mode 'r' at 0x026F6A18>
Traceback (most recent call last):
True
<zipfile.ZipFile object at 0x02736EB0>
  File "C:/Users/Evolution/PycharmProjects/MCMODVERSION/stuff/main.py", line 80, in <module>
<open file 'C:\\Users\\Evolution\\Desktop\\mods\\BC_AdditionalPipes2.6.0-BC4.2.1.jar', mode 'r' at 0x026F6AC8>
    o = t.getdirList()
True
  File "C:/Users/Evolution/PycharmProjects/MCMODVERSION/stuff/main.py", line 36, in getdirList
    self.getModinfo(os.path.join(p, o[n]), 'name')
  File "C:/Users/Evolution/PycharmProjects/MCMODVERSION/stuff/main.py", line 46, in getModinfo
    y = zipfile.ZipFile(x)
  File "C:\Python27\lib\zipfile.py", line 766, in __init__
    self._RealGetContents()
  File "C:\Python27\lib\zipfile.py", line 832, in _RealGetContents
    raise BadZipfile("Truncated central directory")
zipfile.BadZipfile: Truncated central directory

Process finished with exit code 1

正在执行的代码:

def getdirList(self):
    p = 'C:\Users\Evolution\Desktop\mods'
    o = [f for f in listdir(p) if isfile(join(p, f))]
    for mod in range(len(o)):
        print o[mod]
        self.getModinfo(os.path.join(p, o[mod]), 'name')


def getModinfo(self,mod,type):
    """ Types:
    modid, name, description, version, mcversion, url
    updateurl, authors, credits, dependencies """
    x = file(mod)
    print x
    print zipfile.is_zipfile(x)
    y = zipfile.ZipFile(x)
    print y

修正代码:

def getdirList(self):
    p = r'C:\Users\Evolution\Desktop\mods'
    o = [f for f in listdir(p) if isfile(join(p, f))]
    for mod in range(len(o)):
        print o[mod]
        self.getModinfo(os.path.join(p, o[mod]), 'name')

def getModinfo(self,mod,type):
    """ Types: modid, name, description, version, mcversion, 
    url, updateurl, authors, credits, dependencies """
    try:
        x = open(mod, 'rb')
        y = zipfile.ZipFile(x)
        z = y.read(self.minfo)
        #print z
        #zz = self.parseIt(z, '%s": ' % type, ',')
        #print zz
    except KeyError:
        print "ERROR: %s has no %s file!" % (x, self.minfo)
        print "%s" % "-" * 50
    x.close()

现在我运行检查,因为你可以看到zip文件实际上是一个zip文件。超过一半的文件抛出此异常。 .jar和.zip

Now I run a check as you can see that says the zip file is, infact, a zip file. This exception is throw for more than half the files. .jar and .zip

是的我手动打开它们,它们没有损坏或锁定或任何东西。有什么我可以做的或者我可以使用另一个库来操纵python中的achrives吗?

Yes I have manually opened them, they are not corrupt or locked or anything. Is there something I can do or perhaps another library I can use for manipulation of achrives in python?

谢谢。

推荐答案

Zip文件是二进制文件。要正确处理二进制文件,您应该以二进制模式打开文件。

Zip files are binary files. To handle the binary file correctly you should open the file with binary mode.

替换以下行:

x = file(mod)

with:

x = file(mod, 'rb')






BTW,在目录路径中转义 \ ,或使用原始字符串文字。 (这不是问题的直接原因,但有一天逃脱序列会给你带来麻烦)


BTW, escape \ in the directory path, or use raw string literal. (This is not a direct cause of the problem, but someday escape sequence will trouble you)

'C:\\Users\\Evolution\\Desktop\\mods' # escape

r'C:\Users\Evolution\Desktop\mods'    # raw string literal (`r` prefix)

这篇关于Zipfiles的Python问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆