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: 
Try closing the file before removing it.

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

file.close()

remove(lfilename)

```
You must first close the file.

```
    file.close()
    remove(lfilename)

```

Alternatively you could do the following:

```
with ZipFile('lfilename') as file:
    for filename in file.namelist():
        file.extract(filename,dir)
remove(lfilename)

```