GDAL : The process cannot access the file because it is being used by another process

44 Views Asked by At

I'm facing an issue with my code : I want to delete files at the end of my process using the os.remove function but I got this error : The process cannot access the file because it is being used by another process.

In my code I'm using the GDAL package to open the files I want to delete so I'm guessing that's where the problem come from. I tried to close the stream of those files using this pattern : variable = None but this does not worked.

Here is a full overview of my code :

import tarfile
import os
import xarray
import tifffile
import gdal
import numpy as np


def OC3(Band1, Band2, Band3): 
    ratio = np.maximum(Band1, Band2) / Band3
    ChloA = 10**(0.12825 - 2.04833 * ratio - 0.00187 * ratio**2 + 1.28644 * ratio**3 + 0.07052 * ratio**4)
    return ChloA

def NDTI(Band3, Band4): 
    NDTI = (Band4 - Band3) / (Band4 + Band3)
    return NDTI
path = r"\\geo12\students\alengrand\test"
extract_path = r"C:\Users\Alengrand\Desktop\Test"
os.chdir(path)
fname = os.listdir(".")
print(fname)
for i in range(len(fname)) : 
    if fname[i].endswith("tar.gz") : #extraction of all the FOI in the extract_path

        tar = tarfile.open(fname[i], "r:gz")
        files = tar.getnames()
        names = []
        for i in range(len(files)) : 
            if files[i].endswith("AR_BAND1.tif") or files[i].endswith("AR_BAND2.tif") or files[i].endswith("AR_BAND3.tif") or files[i].endswith("AR_BAND4.tif"): 
                names.append(files[i])
        for i in range(len(names)) : 
            tar.extract(names[i], path=extract_path)

    extract_name = os.listdir(extract_path)

    for i in range(len(extract_name)):
        if extract_name[i].endswith("AR_BAND1.tif"):
            Band1_ds = gdal.Open(f'{extract_path}/{extract_name[i]}')
            Band1 = Band1_ds.ReadAsArray().astype(float)  # Convert to float
            Band1[Band1 < 0] = np.nan
        elif extract_name[i].endswith("AR_BAND2.tif"):
            Band2_ds = gdal.Open(f'{extract_path}/{extract_name[i]}')
            Band2 = Band2_ds.ReadAsArray().astype(float)  # Convert to float
            Band2[Band2 < 0] = np.nan
        elif extract_name[i].endswith("AR_BAND3.tif"):
            Band3_ds = gdal.Open(f'{extract_path}/{extract_name[i]}')
            Band3 = Band3_ds.ReadAsArray().astype(float)  # Convert to float
            Band3[Band3 < 0] = np.nan
        elif extract_name[i].endswith("AR_BAND4.tif"):
            Band4_ds = gdal.Open(f'{extract_path}/{extract_name[i]}')
            Band4 = Band4_ds.ReadAsArray().astype(float)  # Convert to float
            Band4[Band4 < 0] = np.nan

    ChloA = OC3(Band1, Band2, Band3)
    Turb = NDTI(Band3, Band4)

    # Get the geotransform and projection from one of the input datasets
    geotransform = Band1_ds.GetGeoTransform()
    projection = Band1_ds.GetProjection()

    # Create a new GeoTIFF file for ChloA
    driver = gdal.GetDriverByName('GTiff')
    ChloA_dataset = driver.Create(r"C:\Users\alengrand\Desktop\ChloA.tif", ChloA.shape[1], ChloA.shape[0], 1, gdal.GDT_Float32)
    ChloA_dataset.SetGeoTransform(geotransform)
    ChloA_dataset.SetProjection(projection)
    ChloA_dataset.GetRasterBand(1).WriteArray(ChloA)
    ChloA_dataset = None  # Close the dataset to flush to disk

    # Create a new GeoTIFF file for Turb
    Turb_dataset = driver.Create(r"C:\Users\alengrand\Desktop\Turb.tif", Turb.shape[1], Turb.shape[0], 1, gdal.GDT_Float32)
    Turb_dataset.SetGeoTransform(geotransform)
    Turb_dataset.SetProjection(projection)
    Turb_dataset.GetRasterBand(1).WriteArray(Turb)
    Turb_dataset = None  # Close the dataset to flush to disk

    print("ChloA and Turb GeoTIFF files saved successfully.")
    Band1 = None
    Band2 = None
    Band3 = None 
    Band4 = None
    ChloA = None
    Turb = None
    
    for filename in os.listdir(extract_path): #delete the files after using them
         if os.path.isfile(os.path.join(extract_path, filename)):
           os.remove(os.path.join(extract_path, filename))
1

There are 1 best solutions below

1
Aurélien Lengrand On

The answer was quite obvious ... I was not closing the dataset as it was called Bandx_ds instead of Bandx.