微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python压缩文件不放开zip文件

我试图在Windows 8.1和Python 2.7.9上使用zipfile库。

我只是想在zipfile.open()后删除library.zip,但os.remove()抛出“WindowsError [错误32]”,似乎zipfile没有释放压缩文件与块。

WindowsError 32的意思是“进程无法访问该文件,因为它正在被另一个进程使用”。

那么,我该如何删除这个library.zip文件呢?

追溯(最近一次通话最后):Adafruit BME 280传感器

Django的。 在此服务器上找不到请求的URL /

PHP + Shell返回HTML

用于SMBIOS的python模块

os.walk不能在path中使用unicode字符

码:

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 if语句总是

从Python的terminalclosures事件中获取信号

将Python脚本移动到另一台计算机

django-gunicorn-Nginx:502坏的网关

RTSPstream和OpenCV(Python)

我想你必须关闭你的压缩文件,然后才能删除它,或者像在python文档中说的那样退出程序https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.close

所以在删除归档之前运行z1.close()和z2.close()

您的代码必须如下所示:

import os import zipfile as z dirs = os.listdir('build/') bSystemStr = dirs[0] print("[-] Merging library.zip...") with z.ZipFile('build/' + bSystemStr + '/library.zip',t[1].read()) except: pass z2.close() z1.close() print("[-] Cleaning temporary files...") os.remove('build_temp/' + bSystemStr + '/library.zip')

如果我错了,纠正我。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐