Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing a dataset to the same file twice results in OSError #1394

Open
alessandraquig opened this issue Jan 7, 2025 · 1 comment
Open

Writing a dataset to the same file twice results in OSError #1394

alessandraquig opened this issue Jan 7, 2025 · 1 comment

Comments

@alessandraquig
Copy link

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.

@ocefpaf
Copy link
Collaborator

ocefpaf commented Jan 8, 2025

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants