Skip to content

User directories (~) not expanded by open_mfdataset(). #4783

Closed
@juseg

Description

@juseg

What happened:

Hi. Perhaps a trivially minor issue, but I have noticed that open_dataset and to_netcdf expand user directories (~), while open_mfdataset does so on file lists, but not on patterns or single files.

What you expected to happen:

I would expect consistent behaviour across all file reading and writing methods.

Minimal Complete Verifiable Example:

>>> import xarray as xr
>>> xr.Dataset().to_netcdf('~/dataset.nc')  # works
>>> xr.open_dataset('~/dataset.nc')         # works
>>> xr.open_mfdataset(['~/dataset.nc'])     # works
>>> xr.open_mfdataset('~/dataset.nc')       # error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/site-packages/xarray/backends/api.py", line 891, in open_mfdataset
    raise OSError("no files to open")
OSError: no files to open

Anything else we need to know?:

This is done by private method _normalize_paths, which is called in open_dataset and to_netcdf but not open_mfdataset.

def _normalize_path(path):
if isinstance(path, Path):
path = str(path)
if isinstance(path, str) and not is_remote_uri(path):
path = os.path.abspath(os.path.expanduser(path))
return path

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.9.1 (default, Dec 13 2020, 11:55:53)
[GCC 10.2.0]
python-bits: 64
OS: Linux
OS-release: 5.4.80-2-MANJARO
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_GB.utf8
LOCALE: en_GB.UTF-8
libhdf5: 1.12.0
libnetcdf: 4.7.4

xarray: 0.16.1
pandas: 1.1.5
numpy: 1.19.4
scipy: 1.5.4
netCDF4: 1.5.4
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.2.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.30.0
distributed: None
matplotlib: 3.3.3
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 51.0.0
pip: 20.2.2
conda: None
pytest: 6.1.2
IPython: 7.19.0
sphinx: 3.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions