My code keeps throwing up a CPLE_AppDefinedError when using rasterio

Here is the issue. When I try to delete a temp.tiff file I no longer need, I get an error with this section of code. It is part of a for loop, and there’s something in this for loop that is causing the code to not want to delete the temp.tiff file. It throws up a permission denied error because it thinks it is already open elsewhere so it can’t then delete it. I can’t figure out the issue.


  
    SAR_iDPolRAD_images_filenames_list = []
  
    for slice_index in range(number_of_images_to_examine_per_row**2):
        
        
        SAR_iDPolRAD_images_filenames_list.append(r'C:Usersbaileyj5DocumentsSatellite_dataImage_datafile_index_{0}SAR_iDPolRAD_image_for_file_index_{0}_for_slice_index_{1}.png'.format(
            file_index,slice_index))
        
        
        if os.path.isfile(r'C:Usersbaileyj5DocumentsSatellite_dataImage_datafile_index_{0}SAR_iDPolRAD_image_for_file_index_{0}_for_slice_index_{1}.png'.format(file_index,slice_index)):
            continue
        
       
        with rasterio.open(imfile) as dataset_full:
            cutout=from_bounds(merged_x_physical_coord_list_left_side[slice_index], 
                               merged_y_physical_coord_list_left_side[slice_index], 
                               merged_x_physical_coord_list_right_side[slice_index], 
                               merged_y_physical_coord_list_right_side[slice_index], 
                               dataset_full.transform)#To cut out in Earth coordinates
            dataset_w = dataset_full.read(1,window=cutout)#Cut out in georeferenced coordinates - i.e. the bounds are the georeferenced boundaries in metres.
            win_transform = dataset_full.window_transform(cutout)#tells you the Affine matrix of the window
            

        pixelSizeX = win_transform[0]#pixel size of window x metres
        pixelSizeY =-win_transform[4]#pixel size of window y metres
        #print('pixel size of window x metres',pixelSizeX)#comment this out if you want.
        #print('pixel size of window y metres',pixelSizeY)#comment this out if you want.

       
        datasetfromwindow = rasterio.open(
            r'C:Usersbaileyj5DocumentsSatellite_dataImage_datafile_index_{0}temp.tiff'.format(file_index),
            'w',driver='GTiff',height=dataset_w.shape[0],width=dataset_w.shape[1],count=1,dtype=dataset_w.dtype,crs='EPSG:32640',transform=win_transform)
       
        
        datasetfromwindow.write(dataset_w, 1)
       
        datasetfromwindow.close()

      
        datasetfromwindow_read = rasterio.open(
        r'C:Usersbaileyj5DocumentsSatellite_dataImage_datafile_index_{0}temp.tiff'.format(file_index))
        

One of the first things I tried was to make a small code in another script that would delete the temp.tiff file using os.remove which worked just fine. So I know something in this big code is throwing the error. And the debugging shows that it occurs within the for loop.

It happens at this section here:

 datasetfromwindow = rasterio.open(
            r'C:Usersbaileyj5DocumentsSatellite_dataImage_datafile_index_{0}temp.tiff'.format(file_index),
            'w',driver='GTiff',height=dataset_w.shape[0],width=dataset_w.shape[1],count=1,dtype=dataset_w.dtype,crs='EPSG:32640',transform=win_transform)

And this is the error it will throw up:

CPLE_AppDefinedError: Deleting C:/Users/baileyj5/Documents/Satellite_data/Image_data/file_index_0/temp.tiff failed: Permission denied

Any help with this would be really helpful. Cheers.

I’ve attached a screenshot of the code as well.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật