Search code examples
pandasloopspython-xarraygfs

How to open multiple files in a path skiiping files with a determined name


I have a folder with files whose name goes from f000 to f168, and I would like to select only files above f000, that is, from f003, f006, to f168. How can I do this ?

The file name starts like this:

gfs.0p25.2020010100.f000.WE.grib2


Solution

  • As suggested above, you can always filter the list of files before passing them to open_mfdataset:

    import glob
    import xarray as xr
    
    files = glob.glob('/path/to/files/*grib2')
    filtered = [f for f in files if 'f000' in f]
    
    ds = xr.open_mfdataset(filtered)
    

    Alternatively, you may be able to tune your glob string to do this automatically:

    ds = xr.open_mfdataset('/path/to/files/*.f000.*.grib2')