Error: PermissionError: [WinError 32] another program is using this file, the process can not access. "+ File path" solution

When using python recently screened the picture, think of the picture were deleted using python os inside the library.

Specific screening methods is, deleted or width less than the length of image 100 pixel image, the following sample code:

for file in os.listdir(img_path):
    if file.split('.')[-1]=='jpg':
        img = Image.open(os.path.join(img_path,file))
        #img.close()  bug修改代码
        size = list(img.size)
        if size[0] > 100 or size[1] > 100:
            pass
        else:
            os.remove(os.path.join(img_path,file))
            print(file)

When running the above code, it will report the following error:

Solution: Whether PIL, opencv and other python libraries open a picture, we can not delete operation, just as error, the current file being used by another program, the solution is to add the following line Code

img.close () #bug Modify Code

Thus, the successful resolution of the error.

to sum up:

  First, after either an image file or a txt and other text files, when we conduct open, we must remember to close out, can even extend to just use the open function, you must remember to close out.
  When Second, the time here is just to remove this error will be reported, if it is a picture array to modify or move the picture will not be as it reported this error? The suspect left after verification.

Guess you like

Origin www.cnblogs.com/xiaoyh/p/12036608.html