2015-02-05 11 views
3

windows 8.1 및 python 2.7.9에서 zipfile 라이브러리를 사용하려고합니다.zip 파일을 공개하지 않는 파이썬 zipfile

zipfile.open()하지만 os.remove()가 "WindowsError [Error 32]"를 throw하고 zipfile이 zip 파일을 블록과 함께 해제하지 않은 것으로 보이는 경우 library.zip을 제거하려고합니다.

WindowsError 32는 "프로세스가 다른 프로세스에서 사용 중이기 때문에 파일에 액세스 할 수 없습니다."라는 의미입니다.

그렇다면이 library.zip 파일을 어떻게 제거 할 수 있습니까?

코드 :

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

오류 : 나는 https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.close

그래서 z1.close() 실행이 파이썬 문서에서 말하는 것처럼 당신이 그것을 삭제하거나 프로그램을 종료하기 전에 아카이브를 닫아야합니다 생각

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

zip 파일을 'zipfile'과 함께 여기에서 열지 않으면 삭제할 수 있습니까? 어쩌면 zip 파일을 생성하는 코드 (표시되지 않았 음)가 열려있을 수 있습니다. – interjay

+0

[이 버그] (http://bugs.python.org/issue16183#msg172555)의 간단한 예가 같은 오류를 줍니까? – cpburnz

+1

't [1]'을 통해 파일에 매달린 참조가 있습니다. 루프를 'for n in z2.namelist()'로 다시 작성합니다. 그런 다음'with z2.open (n) t '와'z1.writestr (n, t.read())'를 사용하십시오. 이렇게하면 내부 파일이 자동으로 닫힙니다. – eryksun

답변

0

보관 파일을 삭제하기 전에 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', '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') 

은 저를 수정합니다.