4

我在使用 Python 中的 zipfile 构建的存档时遇到问题。我正在遍历目录中的所有文件并将它们写入存档。当我之后尝试提取它们时,我得到一个与路径分隔符相关的异常。

the_path= "C:\\path\\to\\folder"
zipped= cStringIO.StringIO()
zf = zipfile.ZipFile(zipped_cache, "w", zipfile.ZIP_DEFLATED)
for dirname, subdirs, files in os.walk(the_path) :
    for filename in files:
        zf.write(os.path.join(dirname, filename), os.path.join(dirname[1+len(the_path):], filename))
zf.extractall("C:\\destination\\path")
zf.close()
zipped_cache.close()

这是一个例外:

zipfile.BadZipfile:目录“env\index”和标题“env/index”中的文件名不同。

cStringIO.StringIO()更新:我用一个临时文件( )替换了字符串缓冲区tempfile.mkstemp("temp.zip"),现在它可以工作了。当 zipfile 模块写入损坏存档的缓冲区时,会发生一些事情,但不确定是什么问题。

问题是我正在从/写入以“r”/​​“w”模式而不是“rb”/“wb”模式打开的文件中的信息。这在 Linux 中不是问题,但由于字符编码,它在 Windows 中给了我错误。解决了。

4

4 回答 4

5

您应该考虑在字符串之前添加一个r以表明它是一个原始字符串——路径中的反斜杠被解释为转义字符。

以下代码:

#!/bin/env python    
print(r"C:\destination\path")
print(r"C:\path\to\folder")
print("C:\destination\path")
print("C:\path\to\folder")

产生以下输出:

C:\destination\path
C:\path\to\folder
C:\destination\path
C:\path o
         older

注意 \t 和 \f在最后一行被解释为制表符换页符。

有趣的是,您还可以将反斜杠更改为正斜杠(即open("C:/path/to/folder"),这将起作用。

或者,使用 ... 反斜杠(即open("C:\\path\\to\\folder"))转义反斜杠。

IMO,最清晰和最简单的解决方案是简单地添加一个r


编辑:看起来你需要使用第二种解决方案,正斜杠。zipfile 库显然有点严格——鉴于这是一个仅限窗口的错误,它可能会偷偷溜进来。(见问题 6839)。

于 2011-05-19T19:34:48.357 回答
4

在这里找到了我的问题的答案:http ://www.penzilla.net/tutorials/python/scripting 。

我正在粘贴与压缩目录相关的两个函数。问题不是字符串缓冲区,也不是斜杠,而是我迭代和写入 zipfile 的方式。这两个递归函数解决了这个问题。遍历整个子目录树os.walk并不是编写存档的好方法。

def zippy(path, archive):
    paths = os.listdir(path)
    for p in paths:
        p = os.path.join(path, p) # Make the path relative
        if os.path.isdir(p): # Recursive case
            zippy(p, archive)
        else:
            archive.write(p) # Write the file to the zipfile
    return

def zipit(path, archname):
    # Create a ZipFile Object primed to write
    archive = ZipFile(archname, "w", ZIP_DEFLATED) # "a" to append, "r" to read
    # Recurse or not, depending on what path is
    if os.path.isdir(path):
        zippy(path, archive)
    else:
        archive.write(path)
    archive.close()
    return "Compression of \""+path+"\" was successful!"
于 2011-05-20T23:26:13.840 回答
1

您需要转义路径中的反斜杠。

尝试更改以下内容:

  • the_path= "C:\path\to\folder"the_path = "C:\\path\\to\\folder", 和
  • zf.extractall("C:\destination\path")zf.extractall("C:\\destination\\path").
于 2011-05-19T19:35:25.710 回答
1

即使在 Windows 上,您也可以使用正斜杠作为路径分隔符。我建议您在创建 zip 文件时尝试这样做。

于 2011-05-19T20:05:03.473 回答