import netCDF4 from datetime import datetime import glob import os folder="/work/mh0731/m300382/test_nwp_new/" # in_files="iefff0???0000_2_lbc.nc" # out_prefix="iefff_" for filename in sorted(glob.glob(folder+in_files)): nc_temp = netCDF4.Dataset(filename) dtime = netCDF4.num2date(nc_temp.variables['time'][:], nc_temp.variables['time'].units) date_string = datetime.strftime(dtime[0], format='%Y%m%d%H') print('old = '+filename) print('new = '+folder+out_prefix+date_string+"_lbc.nc") os.rename(filename, folder+out_prefix+date_string+"_lbc.nc")