Error loading data: how to automatically get rid of "bad" files?


I downloaded tracks, some of which have “h_li” and some don’t. I can’t use reader.load() because the reader.vars.append(beam_list=['gt1l', 'gt3r'], var_list=['h_li',"latitude", "longitude"]) command fails.

I decided to sort the complete and incomplete files by looping through each of them, and delete the ones that could not be opened:

import glob as glob
import os
list_files = glob.glob('A:/PHD/Scripts/Icesat/Test/*')  # Gather a list of files

# For each file, open it and see if it can be loaded. If not, delete it.
for name in list_files:
    path_root = name # Grab the file
    pattern = "processed_ATL{product:2}_{datetime:%Y%m%d%H%M%S}_{rgt:4}{cycle:2}{orbitsegment:2}_{version:3}_{revision:2}.h5"
    reader = ipx.Read(path_root, "ATL06", pattern) # or ipx.Read(filepath, "ATLXX") if your filenames match the default pattern
    # Test if the file can be opened
        reader.vars.append(beam_list=['gt1l', 'gt3r'], var_list=['h_li',"latitude", "longitude"])
        print(f'{name} is complete')
        print(f'{name} is incomplete')
        os.remove(name) # Remove the file if incomplete

However it kind of defeats the purpose of using ipx.Read() which can already iterate through the entire folder.
My question is: is there any way to test if the files are incomplete (through ipx.Read() functionalities), and delete them automatically (without looping through all the files) ?

Thank you !