【问题标题】:WinError 32 The process cannot access the file because it is being used by another process | PythonWinError 32 进程无法访问该文件,因为它正被另一个进程使用 | Python
【发布时间】:2021-09-28 00:53:51
【问题描述】:

我每次打开一个文件都很吃力。

作为以下代码的一部分,relevant post 的大多数解决方案要么不适用于我的情况,要么我没有正确实施。

我没有在我系统的其他任何地方打开这些文件或文件夹。我在Windows 10 Pro 上使用Jupyter Notebooks

pip3 install opendatasets
import opendatasets as od
import urllib
import zipfile
import os
import shutil
from PIL import Image
import time

try:
    download = od.download('http://www.mae.cuhk.edu.hk/~cvl/iris_database/iris_database.zip', '../data/')
        
    path_extract = '../data/iris_database/'
    with zipfile.ZipFile('../data/iris_database.zip', 'r') as zip_ref:
        zip_ref.extractall(path_extract)
        
    #time.sleep(30)
    os.remove(path_extract + 'readme.txt')

    filenames = os.listdir(path_extract)
    scans = []
    for f in filenames:
        #img = Image.open(path_extract + f)
        with Image.open(path_extract + f) as img:
            #print("IMG", img)
            matrix = np.array(img)
            #print("MATRIX", matrix)
            scans.append(matrix)
        
    print("before RMTREE")
    shutil.rmtree(path_extract) # REMOVE LINE TO WORK
    print("before OS.REMOVE")
    os.remove(path_extract[:-1] + '.zip')

except (urllib.error.URLError, IOError, RuntimeError) as e:
    print(e)
>>> [WinError 32] The process cannot access the file because it is being used by another process: '../data/iris_database/riris26.bmp'

【问题讨论】:

  • 我有。我怀疑 OneDrive 会妨碍我,但尝试time.sleep(30) 有足够的时间让文件资源管理器的备份状态符号变为绿色。
  • 此外,我刚刚在网络挂载之外使用相同的文件夹结构运行了这个 .ipynb 文件;以防万一,仍然会引发此错误。运行我上面的代码对你有用吗?

标签: python-3.x windows jupyter-notebook file-permissions


【解决方案1】:

删除了 shutil.rmtree(path_extract)。 Python 的当前工作目录位于此文件路径位置。因此,在 os library "points" 指向它时删除父文件夹会导致此错误。

或者,os.chdir('C:/...')Tutorial Link 上回答。

【讨论】:

  • 注意:shutil.rmtree(path_extract) 适用于 repl.it,但不适用于 Jupyter Notebooks,至少在我的情况下。如果有人能详细说明为什么会这样,我会接受他们的回答。
猜你喜欢
  • 2019-11-26
  • 2015-11-13
  • 2018-10-28
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
  • 2021-09-21
  • 2020-10-31
相关资源
最近更新 更多