如何用Python读取存储在多个文件夹中的多个NetCDF文件?
Got it, let's tackle this problem! The core idea is to first collect all the NetCDF file paths across your target folders, then feed that list to xarray.open_mfdataset(). Here are three reliable, easy-to-use approaches:
方法1:用glob递归匹配(Python 3.5+)
The glob module's recursive mode lets you match files in all subfolders with a simple pattern. This is the quickest option for most cases:
import glob import xarray as xr # 匹配根目录下所有子文件夹中的.nc文件 file_paths = glob.glob('F:/netcdf/example/**/*.nc', recursive=True) # 批量合并读取 dsmerged = xr.open_mfdataset(file_paths)
Tip: If you only want to target specific subfolders (not all), adjust the pattern—for example, 'F:/netcdf/example/year*/month/*.nc' to match files in year folders nested under the example directory.
方法2:手动遍历文件夹(高度灵活)
Use os.walk to traverse every directory and collect files manually. This is great if you need to add custom filters (like excluding certain folders or matching specific filename patterns):
import os import xarray as xr root_dir = 'F:/netcdf/example' file_paths = [] # 遍历根目录下的所有文件和子目录 for dirpath, _, filenames in os.walk(root_dir): for filename in filenames: # 只收集.nc文件,可添加额外条件(比如文件名包含"temp") if filename.endswith('.nc'): file_paths.append(os.path.join(dirpath, filename)) # 合并读取 dsmerged = xr.open_mfdataset(file_paths)
方法3:用pathlib(现代路径处理)
The pathlib module offers a more object-oriented way to handle file paths, which is cross-platform and intuitive:
from pathlib import Path import xarray as xr root_path = Path('F:/netcdf/example') # 递归查找所有.nc文件并转换为字符串列表 file_paths = [str(path) for path in root_path.rglob('*.nc')] # 合并读取 dsmerged = xr.open_mfdataset(file_paths)
额外注意事项
- Ensure all your NetCDF files have compatible structures (same variables, dimensions, etc.). If not, you can use parameters like
combine='nested'orconcat_dim='time'to control how xarray merges them. - For large numbers of files, enable parallel processing (requires dask) with
xr.open_mfdataset(file_paths, parallel=True)to speed up reading.
内容的提问来源于stack exchange,提问作者Vishal Singh

