0

I'm struggling with opening one file at a time.

The most relevant post's solution either doesn't work for my case or I have not implemented it properly, as part of the below code.

I don't have these files or folders open anywhere else on my system. I am using Jupyter Notebooks on Windows 10 Pro.

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'
  • I have. I was suspicious that OneDrive was in the way, but trying `time.sleep(30)` was plenty of time for File Explorer's back-up status symbol to turn green. –  Jul 20 '21 at 13:57
  • Further, I just ran this `.ipynb` file with the same folder structure outside of my network mount; just in case, and still raises this error. Does running my above code work for you? –  Jul 20 '21 at 14:01

1 Answers1

0

I removed shutil.rmtree(path_extract). Python's current working directory was at this file path location. So removing the parent folder while os library "points" to it results in this error.

Alternatively, os.chdir('C:/...') as answered on Tutorial Link.

  • 1
    Note: `shutil.rmtree(path_extract)` works on [repl.it](https://replit.com/) but not on `Jupyter Notebooks`, at least in my case. If someone could detail as to why this is I will accept their answer. –  Jul 21 '21 at 09:24