Skip to content

Commit

Permalink
raise better errors if file exist/do not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
headtr1ck committed Nov 14, 2024
1 parent 43ba78a commit 0affb73
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3505,14 +3505,22 @@ Dataset instance for `ncfilename` is returned.
filepath = pathlib.Path(cdlfilename)
if ncfilename is None:
ncfilename = filepath.with_suffix('.nc')
else:
ncfilename = pathlib.Path(ncfilename)
formatcodes = {'NETCDF4': 4,
'NETCDF4_CLASSIC': 7,
'NETCDF3_CLASSIC': 3,
'NETCDF3_64BIT': 6, # legacy
'NETCDF3_64BIT_OFFSET': 6,
'NETCDF3_64BIT_DATA': 5}

if format not in formatcodes:
raise ValueError('illegal format requested')
if not filepath.exists():
raise FileNotFoundError(filepath)
if ncfilename.exists():
raise FileExistsError(ncfilename)

ncgenargs="-knc%s" % formatcodes[format]
subprocess.run(["ncgen", ncgenargs, "-o", str(ncfilename), str(filepath)], check=True)
return Dataset(ncfilename, mode=mode)
Expand Down

0 comments on commit 0affb73

Please sign in to comment.