小编Nes*_*wit的帖子

Python zipfile 不释放 zip 文件

我正在尝试在 Windows 8.1 和 python 2.7.9 上使用zipfile库。

我只想在 zipfile.open() 之后删除library.zip,但 os.remove() 会抛出“WindowsError [Error 32]”,并且 zipfile 似乎不会将 zip 文件从 with 块中释放出来。

WindowsError 32 的意思是“该进程无法访问该文件,因为该文件正在被另一个进程使用。”

那么,如何删除这个library.zip 文件呢?

代码:

import os
import zipfile as z

dirs = os.listdir('build/')
bSystemStr = dirs[0]

print("[-] Merging library.zip...")
with z.ZipFile('build/' + bSystemStr + '/library.zip', 'a') as z1:
    with z.ZipFile('build_temp/' + bSystemStr + '/library.zip', 'r') as z2:
        for t in ((n, z2.open(n)) for n in z2.namelist()):
            try:
                z1.writestr(t[0], t[1].read())
            except:
                pass

print("[-] Cleaning temporary files...")
os.remove('build_temp/' + …
Run Code Online (Sandbox Code Playgroud)

python windows python-2.7 windows-8.1 python-zipfile

5
推荐指数
1
解决办法
4831
查看次数