我的代码遇到问题:我想使用 os.remove 函数在进程结束时删除文件,但出现此错误:该进程无法访问该文件,因为它正在被另一个进程使用。
在我的代码中,我使用 GDAL 包打开我想要删除的文件,所以我猜测这就是问题所在。我尝试使用此模式关闭这些文件的流:
variable = None
但这不起作用。
这是我的代码的完整概述:
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))
答案非常明显......我没有关闭数据集,因为它被称为
Bandx_ds
而不是 Bandx
。