I would like to use itertools.islice(self._f, 0, 100, None) to read in a file piece by piece (in blocks of 100 lines) as follows:
f = open('test.dat', 'r') while (some condition I look for): f = open(fileName, 'r') x = itertools.islice(f, 0, 100, None) doSomethingWithX(x) My problem is, I do not know how long the file is and I am looking for a condition to stop the while loop when the end of the file is reached. But I cannot figure out how it is done.
EDIT: Ok, I see the difficulty. Maybe I should reformulate the question when the itertools.islice is capsuled in a class like here:
class reader: def __init__() self._f = open('test.dat', 'r') def getNext(): return itertools.islice(self._f, 0, 100, None) R = reader() while (some condition I look for): x = R.getNext() doSomethingWithX(x)
linesorbytes?