netcdf4-python icon indicating copy to clipboard operation
netcdf4-python copied to clipboard

Writing a dataset to the same file twice results in OSError

Open alessandraquig opened this issue 10 months ago • 1 comments

When I write a dataset to the same file twice (even with clobber=True), I get an OS error:

import os
from netCDF4 import Dataset

try:
    os.remove('example.nc')
except FileNotFoundError:
    pass

mhw = Dataset('example.nc', mode='w', format='NETCDF4', clobber=True)

# No error if this is uncommented
# os.remove('example.nc')

mhw = Dataset('example.nc', mode='w', format='NETCDF4', clobber=True)

# Traceback (most recent call last):
#   File "/opt/miniconda3/envs/extreme_env/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3577, in run_code
#     exec(code_obj, self.user_global_ns, self.user_ns)
#   File "<ipython-input-16-d19a2f07cbe5>", line 14, in <module>
#     mhw = Dataset('example.nc', mode='w', format='NETCDF4', clobber=True)
#           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Computer: Apple M3 Pro Operating system: macOS version 14.6.1 (23G93)

Library versions (installed using conda):

% conda list | grep "netcdf"
h5netcdf                  1.2.0           py311hca03da5_0  
libnetcdf                 4.8.1                h0fce390_4  
netcdf4                   1.6.2           py311h55fefbe_0 

Permissions for the created file:

% ls - l
-rw-r--r--@ 1 alessandraquig  staff   48 Jan  7 23:31 example.nc

The simple workaround is to delete the file first, but this seems bizarre given the point of clobber=True is to allow me to overwrite an existing file.

alessandraquig avatar Jan 07 '25 23:01 alessandraquig

Clobbering an existing file is different from opening it twice for writing. If you have an example.nc and writes a new one, clobber will work as expected. However, what you are doing is opening it twice and you can prevent that using Python's with statement or closing the file handle.

import os
from netCDF4 import Dataset

# do not delete your example.nc and you'll see it will be clobbered without an error here and than again below. 

with Dataset('example.nc', mode='w', format='NETCDF4', clobber=True) as mhw:
    pass # do something


mhw = Dataset('example.nc', mode='w', format='NETCDF4', clobber=True)
mhw.close()

ocefpaf avatar Jan 08 '25 10:01 ocefpaf