'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'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
