Question: I have some python code to unzip a file and then remove it (the original file), but my code catches an exception: it cannot remove the file, because it is in use.

I think the problem is that when the removal code runs, the unzip action has not finished, so the exception is thrown. So, how can I check the run state of the unzip action before removing the file?

```
file = zipfile.ZipFile(lfilename)
for filename in file.namelist():
    file.extract(filename,dir)
remove(lfilename)

```

Answer: 
The documentation for ZipFile says:

> 
> ZipFile is also a context manager and therefore supports the [with](https://docs.python.org/2/reference/compound_stmts.html#with) statement.
> 
> 
> 

So, I'd recommend doing the following:

```
with zipfile.ZipFile(lfilename) as file:
    file.extract(filename, dir)
remove(lfilename)

```

One advantage of using a with statement is that the file is closed automatically. It is also beautiful (short, concise, effective).

See also [PEP 343](https://www.python.org/dev/peps/pep-0343/).
Try closing the file before removing it.

```
file = zipfile.ZipFile(lfilename)
for filename in file.namelist():
    file.extract(filename,dir)

file.close()

remove(lfilename)

```