Python zipfile 不释放 zip 文件 [英] Python zipfile dosen't release zip file

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

问题描述

我正在尝试在 Windows 8.1 和 python 2.7 上使用 zipfile 库.9.

I'm trying to use zipfile library on windows 8.1 and python 2.7.9.

我只想在 zipfile.open() 之后删除 library.zip 但 os.remove() 抛出WindowsError [错误 32]",而且 zipfile 似乎没有将 zip 文件从块中释放出来.

I just want to remove library.zip after zipfile.open() but os.remove() throws "WindowsError [Error 32]" and it seems zipfile doesn't release the zip file out of with block.

WindowsError 32 表示该进程无法访问该文件,因为它正被另一个进程使用."

WindowsError 32 means "The process cannot access the file because it is being used by another process."

那么,我怎样才能删除这个 library.zip 文件?

So, how can I remove this library.zip file?

代码:

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/' + bSystemStr + '/library.zip')

错误:

[-]Merging library.zip...
...
build.py:74: UserWarning: Duplicate name: 'xml/sax/_exceptions.pyc'
  z1.writestr(t[0], t[1].read())
build.py:74: UserWarning: Duplicate name: 'xml/sax/expatreader.pyc'
  z1.writestr(t[0], t[1].read())
build.py:74: UserWarning: Duplicate name: 'xml/sax/handler.pyc'
  z1.writestr(t[0], t[1].read())
build.py:74: UserWarning: Duplicate name: 'xml/sax/saxutils.pyc'
  z1.writestr(t[0], t[1].read())
build.py:74: UserWarning: Duplicate name: 'xml/sax/xmlreader.pyc'
  z1.writestr(t[0], t[1].read())
build.py:74: UserWarning: Duplicate name: 'xmllib.pyc'
  z1.writestr(t[0], t[1].read())
build.py:74: UserWarning: Duplicate name: 'xmlrpclib.pyc'
  z1.writestr(t[0], t[1].read())
build.py:74: UserWarning: Duplicate name: 'zipfile.pyc'
  z1.writestr(t[0], t[1].read())
[-] Cleaning temporary files...
Traceback (most recent call last):
  File "build.py", line 79, in <module>
    os.remove('build_temp/' + bSystemStr + '/library.zip')
WindowsError: [Error 32] : 'build_temp/exe.win32-2.7/library.zip'

推荐答案

我认为您必须先关闭存档,然后才能删除它或退出程序,如 Python 文档中所述 https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.close

I think you must close your archive before deleting it or exiting the program as it says in python documentation https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.close

因此在删除存档之前运行 z1.close()z2.close()

So run z1.close() and z2.close() before removing an archive

您的代码必须如下所示:

Your code must look like this:

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

         z2.close()

     z1.close()


print("[-] Cleaning temporary files...")
os.remove('build_temp/' + bSystemStr + '/library.zip')

如果我错了,请纠正我.

If I'm wrong, correct me.

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

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