Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: No such file or directory. Unable to write files within for loop in python

I am trying to use the rasterio package in python to divide raster images as part of a for loop (raster image is like a matrix/array of pixels in the geospatial analysis world), but am having directory issues I cannot figure out. I am essentially trying to loop through files in a folder, and divide each by a target file, and then produce unique output product files.

I have the following code:

#I am just interested in files containing these strings
d = ['Blue', 'Red', 'Green']
test = 'Colors/test_value.tif'
folder = Path('Colors')

src = rasterio.open(test) 

#Loop through all the '.tif' files in the folder, but only those that contain 'Blue, Red, or Green in the file name
for f in folder.glob("*.tif"):
    if any(color in f.name for color in d):

        med = rasterio.open(f) 
        new_raster = med.read(1)/src.read(1)

        # write raster
        profile = src.profile
        profile.update(
            dtype=new_raster.dtype,
            count=1,
            compress='lzw')
        #Create the output files with each unique file name
        with rasterio.open(f"Colors/Output_Raster_{f}.tif", 'w', **profile) as dst:
            dst.write(new_raster, 1)

So I have 4 files of interest in my referenced ‘folder’. They are: Raster_Blue.tif, Raster_Red.tif, Raster_Green.tif, and Raster_test.tif. I want to loop through the folder and divide each file by ‘Raster_test.tif’. However, I only want to use the Blue, Red, and Green files for my action, and so I do not want to run ‘Raster_test.tif’ divided by ‘Raster_test.tif’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

And so this code is trying to loop through the files in the folder that contain "Blue", "Red", and "Green" in their file name strings, and for each iteration, divide the specific color file by Raster_test.tif. And then I want to write those output files. The challenge is that for each of these output files, three total, for the three iterations the loop will go through, I want to give them each a unique file name, so the file isn’t just being overwritten each time. And so I want to produce Output_Raster_Blue.tif, Output_Raster_Red.tif, and Output_Raster_Green.tif. However, when I try this code, I receive this error message: RasterioIOError: Attempt to create new tiff file 'Colors/Output_Raster_Colors\Output_Raster_Red.tif.tif' failed: No such file or directory

It seems somehow my target directory and the file name strings just got jumbled and I cannot figure out how to fix this, or why this is occurring. How I can fix my loop so that the correct directory is referenced?

>Solution :

It seems f variable is converted to string in this line rasterio.open(f"Colors/Output_Raster_{f}.tif", 'w', **profile).

So "Colors/Output_Raster_{f}.tif" becomes "Colors/Output_Raster_Colors\Output_Raster_Red.tif.tif" and as you can see, a new incorrect path is created and open function cannot create a file within non-existing directory.

I think correcting f to f.name will be OK.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading