Skip to content

Commit 0affb73

Browse files
committed
raise better errors if file exist/do not exist
1 parent 43ba78a commit 0affb73

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/netCDF4/_netCDF4.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,14 +3505,22 @@ Dataset instance for `ncfilename` is returned.
35053505
filepath = pathlib.Path(cdlfilename)
35063506
if ncfilename is None:
35073507
ncfilename = filepath.with_suffix('.nc')
3508+
else:
3509+
ncfilename = pathlib.Path(ncfilename)
35083510
formatcodes = {'NETCDF4': 4,
35093511
'NETCDF4_CLASSIC': 7,
35103512
'NETCDF3_CLASSIC': 3,
35113513
'NETCDF3_64BIT': 6, # legacy
35123514
'NETCDF3_64BIT_OFFSET': 6,
35133515
'NETCDF3_64BIT_DATA': 5}
3516+
35143517
if format not in formatcodes:
35153518
raise ValueError('illegal format requested')
3519+
if not filepath.exists():
3520+
raise FileNotFoundError(filepath)
3521+
if ncfilename.exists():
3522+
raise FileExistsError(ncfilename)
3523+
35163524
ncgenargs="-knc%s" % formatcodes[format]
35173525
subprocess.run(["ncgen", ncgenargs, "-o", str(ncfilename), str(filepath)], check=True)
35183526
return Dataset(ncfilename, mode=mode)

0 commit comments

Comments
 (0)