Skip to content

filename is encoded to utf8, resulting in broken filenames on windows #686

@MaxBo

Description

@MaxBo

When i try to create a netcdf-file with a filepath, that contains Non-ASCII-characters, Netcdf4 creates broken filenames on windows, because it encodes the filepath in UTF8, which is fine for Linux,. In Windows, the filepath has to be convertet to CP1252.

import netCDF4 as nc4
filepath = r'C:\Plön.nc'
ds = nc4.Dataset(filepath, 'w')
ds.close()

this creates a file
Plön.nc

I could try to avoid Non-ASCII-Characters in .nc filenames, but if i want to create a file in the folder
C:\Plön\

import netCDF4 as nc4
filepath = r'C:\Plön\Ploen.nc'
ds = nc4.Dataset(filepath, 'w')
ds.close()

then i get the confusing error message:
builtins.OSError: Permission denied
Because the folder "Plön" is not recognized correctly.

The solution would be to pass the filesystemencoding to _strencode in
_netCDF4.pyx in line 1796

-  bytestr = _strencode(str(filename))
+ bytestr = _strencode(str(filename), encoding=sys.getfilesystemencoding())

I'll create a pull request.
Max

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions