close

[Solved] Permission: [Win 32] The process cannot access the file because it is being used by another process

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Permission: [Win 32] The process cannot access the file because it is being used by another process in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Permission: [Win 32] The process cannot access the file because it is being used by another process Error Occurs?

Today I get the following error Permission: [Win 32] The process cannot access the file because it is being used by another process in python.

How To Solve Permission: [Win 32] The process cannot access the file because it is being used by another process Error ?

  1. How To Solve Permission: [Win 32] The process cannot access the file because it is being used by another process Error ?

    To Solve Permission: [Win 32] The process cannot access the file because it is being used by another process Error This will make sure to delete im (and close the file) before you get to os.remove.

  2. Permission: [Win 32] The process cannot access the file because it is being used by another process

    To Solve Permission: [Win 32] The process cannot access the file because it is being used by another process Error This will make sure to delete im (and close the file) before you get to os.remove.

Solution 1

Your process is the one that has the file open (via im still existing). You need to close it first before deleting it.

I don’t know if PIL supports with contexts, but if it did:

import os
from PIL import Image

while True:    
    img_dir = r"C:\Users\Harold\Google Drive\wallpapers"
    for filename in os.listdir(img_dir):
        filepath = os.path.join(img_dir, filename)
        with Image.open(filepath) as im:
            x, y = im.size
        totalsize = x*y
        if totalsize < 2073600:
            os.remove(filepath)

This will make sure to delete im (and close the file) before you get to os.remove.

If it doesn’t you might want to check out Pillow, since PIL development is pretty much dead.

Solution 2

I was running into the same problem, but the error was intermittent. If you are coding your file open/close correctly and still running into this error, make sure you are not synching the files with Dropbox, Google Drive, etc. I paused Dropbox and I no longer see the error.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read