npfile is DEPRECATED!!
Class for reading and writing numpy arrays to/from files
- Inputs:
- file_name – The complete path name to the file to open
- or an open file-like object
- permission – Open the file with given permissions: (‘r’, ‘w’, ‘a’)
- for reading, writing, or appending. This is the same as the mode argument in the builtin open command.
- format – The byte-ordering of the file:
- ([‘native’, ‘n’], [‘ieee-le’, ‘l’], [‘ieee-be’, ‘B’]) for native, little-endian, or big-endian respectively.
- Attributes:
- endian – default endian code for reading / writing order – default order for reading writing (‘C’ or ‘F’) file – file object containing read / written data
- Methods:
- seek, tell, close – as for file objects rewind – set read position to beginning of file read_raw – read string data from file (read method of file) write_raw – write string data to file (write method of file) read_array – read numpy array from binary file data write_array – write numpy array contents to binary file
Example use: >>> from StringIO import StringIO >>> import numpy as np >>> from scipy.io import npfile >>> arr = np.arange(10).reshape(5,2) >>> # Make file-like object (could also be file name) >>> my_file = StringIO() >>> npf = npfile(my_file) >>> npf.write_array(arr) >>> npf.rewind() >>> npf.read_array((5,2), arr.dtype) >>> npf.close() >>> # Or read write in Fortran order, Big endian >>> # and read back in C, system endian >>> my_file = StringIO() >>> npf = npfile(my_file, order=’F’, endian=’>’) >>> npf.write_array(arr) >>> npf.rewind() >>> npf.read_array((5,2), arr.dtype)
You can achieve the same effect as using npfile, using ndarray.tofile and numpy.fromfile.
Even better you can use memory-mapped arrays and data-types to map out a file format for direct manipulation in NumPy.