pythonnetcat

After converting multiple dataframes into respective nc files how to save those nc files with the original names and a word in windows using python


I have some nc files.Their names are File1.nc, File2.nc, File3.nc, etc. I have extracted data for every nc file, and I have made dataframe for each nc file after doing some calculation. Now after making the dataframes, I want to save them as new nc file with a path and a word adding at the last of the original name. What I want to say is like if the path is "c:/my document/convert", then the new nc file name will be c:/my document/convert/File1_calculated.nc.

what I have done so far for a single file is:

original_name="c:/my document/File1.nc"

prefix = original_name.rsplit('.')[0]
fileoutput = prefix + '_calculated.nc

path_name='c:/my document/convert'

output_name=os.path.join(path_name,fileoutput)

xr.Dataset(df.to_xarray()).to_netcdf(output_name)

This code runs. But when I check the convert folder. It shows empty. Please help me.

In my code 'df' is the dataframe which I made after calculation for the file File1.nc. I use windows operating system.


Solution

  • Using your code you are not getting just File1 as a prefix:

    original_name="c:/my document/File1.nc"
     
    prefix = original_name.rsplit('.')[0]
    
    print(prefix)
    'c:/my document/File1'
    

    you may do instead:

    fn_prefix = original_name.split("/")[-1][:-3]
    fileoutput = fn_prefix + '_calculated.nc