简体   繁体   English

Python zipfile不会发布zip文件

[英]Python zipfile dosen't release zip file

I'm trying to use zipfile library on windows 8.1 and python 2.7.9. 我正在尝试在Windows 8.1和python 2.7.9上使用zipfile库。

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. 我只是想在zipfile.open()之后删除library.zip,但os.remove()抛出“WindowsError [错误32]”并且似乎zipfile不会释放zip文件。

WindowsError 32 means "The process cannot access the file because it is being used by another process." WindowsError 32的意思是“进程无法访问该文件,因为它正由另一个进程使用。”

So, how can I remove this library.zip file? 那么,我该如何删除这个library.zip文件呢?

code: 码:

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')

error: 错误:

[-]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'

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 我认为你必须在删除它之前关闭你的存档,或者如python文档中所说的那样退出程序https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.close

So run z1.close() and z2.close() before removing an archive 因此在删除存档之前运行z1.close()z2.close()

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. 如果我错了,请纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM