无法在 Python 中以编辑模式打开 netCDF 文件

问题描述 投票:0回答:1

我正在尝试使用 netCDF4 库在 Python 中以编辑模式打开 netCDF 文件。我收到的错误是这样的:

OSError: [Errno -101] NetCDF: HDF error: b'example.nc'
操作系统:Ubuntu 22:04 IDE:VS 代码 还值得注意的是,我在 VS Code 中使用了 Jupyter。 我的代码:

import netCDF4 as nc
import h5py
import os
os.getcwd()
file = 'example.nc'
dataset = nc.Dataset(file, 'r+')

完整错误:

OSError                                   Traceback (most recent call last)
Cell In[3], line 6
      4 os.getcwd()
      5 file = 'example.nc'
----> 6 dataset = nc.Dataset(file, 'r+') # Open the netCDF file in read mode
      7 print(dataset)

File src/netCDF4/_netCDF4.pyx:2463, in netCDF4._netCDF4.Dataset.__init__()

File src/netCDF4/_netCDF4.pyx:2026, in netCDF4._netCDF4._ensure_nc_success()

OSError: [Errno -101] NetCDF: HDF error: b'example.nc'

我已经检查过该文件是否具有读写权限,并且确实具有。所以这可能不是问题。 Image of the Error

python netcdf netcdf4 oserror
1个回答
0
投票

如果您尝试打开一个文件进行读写(使用

OSError: [Errno -101] NetCDF: HDF error
),则可能会出现错误
r+
,但您只能读取该文件。

要解决此问题:

  • 如果您只需要读取文件,则使用
    r
    而不是
    r+
  • 如果您需要就地编辑文件,请在使用
    chmod 755 myfile.nc
    打开文件之前更改权限(在 Linux 上使用类似
    r+
    的内容)。
  • 如果您无法更改权限,您可能想尝试使用 xarray 而不是 netcdf4-python。
© www.soinside.com 2019 - 2024. All rights reserved.