Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve "OSError: telling position disabled by next() call"

I am creating a file editing system and would like to make a line based tell() function instead of a byte based one. This function would be used inside of a "with loop" with the open(file) call. This function is part of a class that has:

self.f = open(self.file, 'a+') # self.file is a string that has the filename in it 

The following is the original function (It also has a char setting if you wanted line and byte return):

def tell(self, char=False):     t, lc = self.f.tell(), 0     self.f.seek(0)     for line in self.f:         if t >= len(line):             t -= len(line)             lc += 1         else:             break     if char:         return lc, t     return lc 

The problem I'm having with this is that this returns an OSError and it has to do with how the system is iterating over the file but I don't understand the issue. Thanks to anyone who can help.

like image 924
Brandon H. Gomes Avatar asked Apr 14 '15 03:04

Brandon H. Gomes


1 Answers

I don't know if this was the original error but you can get the same error if you try to call f.tell() inside of a line-by-line iteration of a file like so:

with open(path, "r+") as f:   for line in f:     f.tell() #OSError 

which can be easily substituted by the following:

with open(path, mode) as f:   line = f.readline()   while line:     f.tell() #returns the location of the next line     line = f.readline() 
like image 183
Héctor Avatar answered Sep 17 '22 01:09

Héctor