"""Minimal, safe reader for .npy files (no numpy, no pickle). Refuses object arrays.""" import ast, struct, array, sys def read_npy(path): with open(path, 'rb') as fh: raw = fh.read() assert raw[:6] == b'\x93NUMPY', 'not an npy file' major = raw[6] if major == 1: hl = struct.unpack('' and a.itemsize > 1: a.byteswap() return header, a if kind.startswith('U'): w = int(kind[1:]); out = [] for i in range(n): s = body[i*4*w:(i+1)*4*w].decode('utf-32-le').rstrip('\x00'); out.append(s) return header, out if kind.startswith('S'): w = int(kind[1:]); return header, [body[i*w:(i+1)*w].rstrip(b'\x00').decode('latin1') for i in range(n)] if kind.startswith('M8'): a = array.array('q'); a.frombytes(body[:n*8]); return header, a raise ValueError('unhandled dtype %r' % descr) if __name__ == '__main__': sys.stdout.reconfigure(encoding='utf-8') for p in sys.argv[1:]: h, a = read_npy(p) import os; print(os.path.basename(p), h, 'len', len(a), 'first', a[:3], 'last', a[-3:])