尝试打开 .tif 文件时找不到文件错误

File Not Found error when trying to open .tif file

我正在尝试为我的项目打开 .tif 光栅文件,但 Python 似乎无法在我的计算机上找到它。我正在使用 Mac OS.

这是代码:

import netCDF4 as nc

rasterfile = ('~/Desktop/sds cw/tx_ens_mean_0.25deg_reg_2011-2021_v24.0e.nc')
rasterdata = nc.Dataset(rasterfile)

这是错误:

FileNotFoundError: [Errno 2] No such file or directory: '~/Desktop/sds cw/tx_ens_mean_0.25deg_reg_2011-2021_v24.0e.nc'

任何解决方案将不胜感激。 谢谢

你必须扩展路径。

import pathlib
path = pathlib.Path('~/Desktop/sds cw/tx_ens_mean_0.25deg_reg_2011-2021_v24.0e.nc')
rasterdata = nc.Dataset(path.expanduser())