I would like to write and read Python array from a txt file. I know to how to write it, but reading the array requires me to provide an argument about the length of the array. I do not the length of the array in advance, is there a way to read the array without computing its length. My work in progress code below. If I provide 1 as the second argument in a.fromfile, it reads the first element of the array, I would like all elements to read (basically the array to be recreated).
from __future__ import division
from array import array
L = [1,2,3,4]
indexes = array('I', L)
with open('indexes.txt', 'w') as fileW:
indexes.tofile(fileW)
a = array('I', [])
with open('indexes.txt', 'r') as fileR:
a.fromfile(fileR,1)