diff options
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r-- | numpy/lib/npyio.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 07a8ca429..3e3d00c57 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -195,6 +195,12 @@ class NpzFile(object): else: self.fid = None + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.close() + def close(self): """ Close the file. @@ -306,6 +312,13 @@ def load(file, mmap_mode=None): - If the file is a ``.npz`` file, then a dictionary-like object is returned, containing ``{filename: array}`` key-value pairs, one for each file in the archive. + - If the file is a ``.npz`` file, the returned value supports the context + manager protocol in a similar fashion to the open function:: + + with load('foo.npz') as data: + a = data['a'] + + The underlyling file descriptor is always closed when exiting the with block. Examples -------- |