8000 numpy.ndarray.tofile creates a corrupt file for large arrays in append file mode · Issue #8329 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
8000
numpy.ndarray.tofile creates a corrupt file for large arrays in append file mode #8329
Closed
@potocpav

Description

@potocpav

When saving a large array (>= 2,097,152 doubles), the tofile function creates a file twice the size of the data, and the first half of the file is all zeros. This occurs only in the append file mode ("ab").

My system:

Linux 4.8.10-1-ARCH x86_64
Python 3.5.2
numpy.version.version == '1.11.2'

Code to reproduce:

import numpy

def foo(n, file, mode):
    m = numpy.random.rand(n)
    open(file, "wb").close() # delete old file contents
    f = open(file, mode)
    m.tofile(f)
    f.close()

    n = numpy.fromfile(open(file, "rb"))
    print("correct" if numpy.array_equal(m, n) else "incorrect")

foo(2097151, "f1", "ab") # correct
foo(2097152, "f2", "ab") # incorrect
foo(2097152, "f3", "wb") # correct```

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0