I'm trying to make a program that inserts log entries into a text file. The issue I'm having is that I read through the file line by line for a specific line and want to write before the line. Python correctly reads the line I'm looking for, however, when I seek to go back to the previous position, it does not read the entire line anymore. I checked the offset and it's exactly the same but for some reason the entire line isn't getting read.
def fileWriter():
immediateTrigger = 0
returnTrigger = 0;
with open('C:\\testData.txt', 'r+') as file:
for line in iter(file.readline, ''):
#line = file.readline()
if 'Beginning of text entries' in line:
print('arrived at text entries')
print(file.tell())
print(line)
immediateTrigger = 1
file_pos = file.tell()
while not line.strip() and immediateTrigger == 1:
#print('here')
prev_pos = file.tell()
print(str(prev_pos) + 'before')
newLine = file.readline()
print(newLine)
if 'Text Entry 25' in newLine:
file.seek(prev_pos)
print(str(file.tell()) + 'after')
print(file.readline()')
immediateTrigger = 0
The output I would get is:
arrived at text entries
Text Entry 1: The 1st revision
(random entries...)
36800 before
Text Entry 25: The 25th revision
36800 after
try 25: The 25th revision
Why does it cut off like this?
from Not reading all characters after seek
No comments:
Post a Comment