Skip to content

Allow pathlib.Path arguments #6418

@wackywendell

Description

@wackywendell

savez, savez_compressed, and load all take str as an argument, but give an error when passed a pathlib.Path argument as a filename. Now that pathlib.Path is in the standard library, it would be nice if these functions could accept a Path instance as an argument, in addition to str and file objects.

Example:

p = Path('test.npz')
np.savez_compressed(p, data=[10])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-304bca81cdf4> in <module>()
      1 p = Path('test.npz')
----> 2 np.savez_compressed(p, data=[10])

/usr/lib/python3.5/site-packages/numpy/lib/npyio.py in savez_compressed(file, *args, **kwds)
    510 
    511     """
--> 512     _savez(file, args, kwds, True)
    513 
    514 

/usr/lib/python3.5/site-packages/numpy/lib/npyio.py in _savez(file, args, kwds, compress)
    550                 fid.close()
    551                 fid = None
--> 552                 zipf.write(tmpfile, arcname=fname)
    553             finally:
    554                 if fid:

/usr/lib/python3.5/zipfile.py in write(self, filename, arcname, compress_type)
   1481                 zip64 = self._allowZip64 and \
   1482                     zinfo.file_size * 1.05 > ZIP64_LIMIT
-> 1483                 self.fp.write(zinfo.FileHeader(zip64))
   1484                 file_size = 0
   1485                 while 1:

/usr/lib/python3.5/zipfile.py in write(self, data)
    675 
    676     def write(self, data):
--> 677         n = self.fp.write(data)
    678         self.offset += n
    679         return n

AttributeError: 'PosixPath' object has no attribute 'write'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions