简体   繁体   中英

Python zipfile dosen't release zip file

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

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 means "The process cannot access the file because it is being used by another process."

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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