5

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'
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nesswit
  • 53
  • 1
  • 4
  • Can you delete the zip file if you don't open it with `zipfile` at all here? Maybe the code that is creating the zip file (which you haven't shown) is keeping it open. – interjay Feb 05 '15 at 17:40
  • Does the short example from [this bug](http://bugs.python.org/issue16183#msg172555) give you the same error? – Uyghur Lives Matter Feb 05 '15 at 18:31
  • 2
    You have a dangling reference to the file via `t[1]`. I'd rewrite the loop as `for n in z2.namelist()`. Then use `with z2.open(n) as t` and `z1.writestr(n, t.read())`. This way the inner file is closed automatically. – Eryk Sun Feb 05 '15 at 19:51

1 Answers1

2

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.

  • close() is called automatically by the context manager as of python 2.7 https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile – AJ Slater Apr 30 '22 at 03:36