Python PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27215462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:31:56  来源:igfitidea点击:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

pythonpython-3.xfile-handling

提问by user2885647

My code is for a script that looks at a folder and deletes images that are under a resolution of 1920x1080. The problem I am having is that when my code runs;

我的代码用于查看文件夹并删除分辨率为 1920x1080 的图像的脚本。我遇到的问题是当我的代码运行时;

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)
        im = Image.open(filepath)
        x, y = im.size
        totalsize = x*y
        if totalsize < 2073600:
            os.remove(filepath)

I get this error message:

我收到此错误消息:

Traceback (most recent call last):
  File "C:\Users\Harold\Desktop\imagefilter.py", line 12, in <module>
    os.remove(filepath)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\Harold\Google Drive\wallpapers\Car - ABT Audi RS6-R [OS] [1600x1060].jpg'

Just to confirm, Python is the only program running on my computer. What is causing this problem and how do I fix it?

确认一下,Python 是我电脑上唯一运行的程序。是什么导致了这个问题,我该如何解决?

采纳答案by Mike DeSimone

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

您的进程是打开文件的进程(通过im仍然存在)。您需要先关闭它,然后才能删除它。

I don't know if PIL supports withcontexts, but if it did:

我不知道 PIL 是否支持with上下文,但如果它支持:

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.

这将确保im在您到达之前删除(并关闭文件)os.remove

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

如果没有,您可能想查看 Pillow,因为 PIL 开发几乎已经死了。

回答by Cleverlike

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.

我遇到了同样的问题,但错误是间歇性的。如果您正确编码打开/关闭文件但仍然遇到此错误,请确保您没有将文件与 Dropbox、Google Drive 等同步。我暂停了 Dropbox,我不再看到错误。

回答by Ayesha

This is basically permission error, you just need to close the file before removing it. After getting the image size info, close the image with

这基本上是权限错误,您只需要在删除之前关闭文件。获取图像大小信息后,关闭图像

im.close()

回答by fabio.porsche

I also had the issue, in windows [WinError 32]

我也有这个问题,在 Windows [WinError 32]

Solved by changing:

通过更改解决:

try:
    f = urllib.request.urlopen(url)
    _, fname = tempfile.mkstemp()
    with open(fname, 'wb') as ff:
        ff.write(f.read())
    img = imread(fname)
    os.remove(fname)
    return img

into:

进入:

try:
    f = urllib.request.urlopen(url)
    fd, fname = tempfile.mkstemp()
    with open(fname, 'wb') as ff:
        ff.write(f.read())
    img = imread(fname)
    os.close(fd)
    os.remove(fname)
    return img

As found here: https://no.coredump.biz/questions/45042466/permissionerror-winerror-32-when-trying-to-delete-a-temporary-image

如此处所示:https: //no.coredump.biz/questions/45042466/permissionerror-winerror-32-when-trying-to-delete-a-temporary-image