5

I'm attempting to remove a zipped file after unzipping the contents on windows. The contents can be stored in a folder structure in the zip. I'm using the with statement and thought this would close the file-like object (source var) and zip file. I've removed lines of code relating to saving the source file.

import zipfile
import os

zipped_file = r'D:\test.zip'

with zipfile.ZipFile(zipped_file) as zip_file:
    for member in zip_file.namelist():
        filename = os.path.basename(member)
        if not filename:
            continue
        source = zip_file.open(member)

os.remove(zipped_file)

The error returned is:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'D:\\test.zip'

I've tried:

  • looping over the os.remove line in case it's a slight timing issue
  • Using close explicitly instead of the with statment
  • Attempted on local C drive and mapped D Drive
skyking
  • 13,817
  • 1
  • 35
  • 57
Stagg
  • 2,660
  • 5
  • 34
  • 32

4 Answers4

9

instead of passing in a string to the ZipFile constructor, you can pass it a file like object:

import zipfile
import os

zipped_file = r'D:\test.zip'

with open(zipped_file, mode="r") as file:
    zip_file = zipfile.ZipFile(file)
    for member in zip_file.namelist():
        filename = os.path.basename(member)
        if not filename:
            continue
        source = zip_file.open(member)

os.remove(zipped_file)
James Kent
  • 5,763
  • 26
  • 50
  • 1
    documentation for behaviour here: https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.open – Stagg Jan 26 '16 at 10:44
  • This doesn't work for me in Python 3 on Windows 7. I get `raise BadZipFile`. I suspect I may have a gzip file, not a zip file. Could you please show the corresponding code for a gzip file? – Adrian Keister Jul 31 '18 at 15:39
  • 1
    I may have made mistake in the above post, try changing the mode to rb instead – James Kent Jul 31 '18 at 15:47
  • @JamesKent: Still no joy. I'm not sure it is a gzip file - it may just be an ordinary zip file. – Adrian Keister Jul 31 '18 at 17:13
  • Sorry, can't help you then, this code should open a regular zip file without issue, have ypu tried examining your file with other software to identify the compression? – James Kent Jul 31 '18 at 17:24
  • 1
    @JamesKent: I think the problem was that LabVIEW (where I generated the zip file) was still open and hanging on to the file. See my question here: https://stackoverflow.com/questions/51617994/how-do-i-delete-a-gzip-file-in-windows-via-python-file-generated-in-labview. – Adrian Keister Jul 31 '18 at 17:28
2

You are opening files inside the zip... which create a file lock on the whole zip file. close the inner file open first... via source.close() at the end of your loop

import zipfile
import os

zipped_file = r'D:\test.zip'

with zipfile.ZipFile(zipped_file) as zip_file:
    for member in zip_file.namelist():
        filename = os.path.basename(member)
        if not filename:
            continue
        source = zip_file.open(member)
    source.close()

os.remove(zipped_file)
James
  • 414
  • 1
  • 4
  • 12
  • Works for me. Without the source.close() line I get the error "The process cannot access the file because it is being used by another process: 'D:\\test.zip'" on line 14. Adding this line resolve this as there is no longer a file lock on the outer zip file test.zip due to the inner file (source = zip_file.open(member)) being left open. Why open the inner file anyway? What are you trying to do? – James Jan 26 '16 at 10:33
  • I want to open the inner file as I copy the file out of the zip to the parent folder. `target = file(os.path.join(unzip_location, filename), 'wb')` `with source, target:` `shutil.copyfileobj(source, target)` – Stagg Jan 26 '16 at 10:36
  • you on python 2.7, windows 7? – Stagg Jan 26 '16 at 10:42
  • @eryksun: Could you please expand your comment into an answer? – Adrian Keister Jul 31 '18 at 15:20
0

Try to close the zipfile before removing.

Jos
  • 1,387
  • 1
  • 13
  • 27
0

you can do also like this, which works pretty good:

import os, shutil, zipfile

fpath= 'C:/Users/dest_folder'
path = os.getcwd()

for file in os.listdir(path):
    if file.endswith(".zip"):
        dirs = os.path.join(path, file)

if os.path.exists(fpath):
    shutil.rmtree(fpath)
_ = os.mkdir(fpath)

with open(dirs, 'rb') as fileobj:
    z = zipfile.ZipFile(fileobj)
    z.extractall(fpath)
    z.close()
os.remove(dirs)
jyson
  • 245
  • 1
  • 8
  • 27