pythonloopssentinel2

How to select only a file type with os.listdir?


after having concatenated 10 strips of the same image, I want to convert them into reflectance and therefore divide them by 10,000. Nevertheless I have two types of files in my folders, except I want to apply my code only to my.img file and not to the.hdr... Do you know how I can proceed to make this selection with os.listdir?

my code is as follows :

import os
import spectral as sp 
import spectral.io.envi as envi

src_directory = "/d/afavro/Bureau/3_stack/"
dossier = os.listdir (src_directory)
print(dossier)

for fichier in dossier:
    print (fichier)


    ssrc_directory = "/d/afavro/Bureau/3_stack/" + fichier 
    rasters = os.listdir (ssrc_directory) 
    print(rasters) 


    OUTPUT_FOLDER = "/d/afavro/Bureau/4_reflectance/" + 'reflectance_' + fichier
    print(OUTPUT_FOLDER)
    if not os.path.exists(OUTPUT_FOLDER):
        os.makedirs(OUTPUT_FOLDER)

    for image in rasters:
        print (image)

        img = sp.open_image(image)
        print("%s opened successfully" %os.path.basename(image))
        im_HS = img[:,:,:]/10000

        header = envi.read_envi_header('/d/afavro/Bureau/3_stack/'+ image)

        #Save index image
        sp.envi.save_image(OUTPUT_FOLDER + '/reflectance_' + image, im_HS, metadate = header, force = True, interleave = 'bsq')

I think that making a yew loop would be a good idea but I don't know how to do it... Ideas ?


Solution

  • Find the extension of the file using os.path.splitext

    for f in os.listdir('<path>'):
        name, ext = os.path.splitext(f)
        if ext == '.img':
            #do stuff