Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PermissionError:[WinError 32]进程无法访问该文件,因为另一个进程正在使用该文件_Python_Python 3.x_File Handling - Fatal编程技术网

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

Python PermissionError:[WinError 32]进程无法访问该文件,因为另一个进程正在使用该文件,python,python-3.x,file-handling,Python,Python 3.x,File Handling,我的代码用于查看文件夹并删除分辨率为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)

我的代码用于查看文件夹并删除分辨率为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)
导入操作系统
从PIL导入图像
尽管如此:
img\u dir=r“C:\Users\Harold\Google Drive\wallpaps”
对于os.listdir(img_dir)中的文件名:
filepath=os.path.join(img_dir,文件名)
im=Image.open(文件路径)
x、 y=im尺寸
总尺寸=x*y
如果总尺寸<2073600:
删除(文件路径)
我收到以下错误消息:

回溯(最近一次呼叫最后一次):
文件“C:\Users\Harold\Desktop\imagefilter.py”,第12行,在
删除(文件路径)
PermissionError:[WinError 32]进程无法访问该文件,因为另一个进程正在使用该文件:“C:\\Users\\Harold\\Google Drive\\Wallpaps\\Car-ABT Audi RS6-R[OS][1600x1600]。jpg”

我想确认一下,Python是我电脑上唯一运行的程序。导致此问题的原因以及如何解决此问题?

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

我不知道PIL是否支持带有上下文的
,但它是否支持:

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)
导入操作系统
从PIL导入图像
尽管如此:
img\u dir=r“C:\Users\Harold\Google Drive\wallpaps”
对于os.listdir(img_dir)中的文件名:
filepath=os.path.join(img_dir,文件名)
使用Image.open(filepath)作为im:
x、 y=im尺寸
总尺寸=x*y
如果总尺寸<2073600:
删除(文件路径)
这将确保在进入
os之前删除
im
(并关闭文件)。删除


如果没有,您可能想检查一下枕头,因为PIL开发几乎已经死了。

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

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

im.close()

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

通过更改解决:

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
进入:


如图所示:

希望这会有所帮助

import os

def close():

    try:
        os.system('TASKKILL /F /IM excel.exe')

    except Exception:
        print("KU")

close()

这并不是在所有情况下都有效。例如,如果我用代码
file\u path=os.path.join(os.path.expanduser(“~/Desktop”),“my\u file.zip”)
构造一个路径,然后尝试
file\u path.close()
我得到AttributeError:“str”对象没有属性“close”。@adriankeester,这是因为您为file\u路径分配了一个“str”对象,而不是“file”对象。对于一个经验丰富的Python程序员来说,这显然是一个错误。这个答案是一个完全可以接受的关闭文件的方法。然而,它不像在公认的答案中提到的
上下文中使用
那样惯用。
import os

def close():

    try:
        os.system('TASKKILL /F /IM excel.exe')

    except Exception:
        print("KU")

close()