Hi all,
I’m trying to implement a function for recording the last read line during text file readline, like read and process per line before leaving the loop and opening on the end point of the last read line (the file doesn’t need to be closed). What is the most beautiful way to do this? Much appreiated!
I’m a bit confused here.
Do you want the last line in the file?
line = ''
with open(filename) as fp:
for line in fp:
pass
Or do you want to relocate your file pointer to the start of the last line in the file?
with open(filename) as fp:
offset = 0
for line in fp:
offset = fp.tell()
with open(filename) as fp:
fp.seek(offset)
# Use fp here: it's at the start of the last line.
This is really unclear. Please show an example of what you want to do.
Thanks for your help.
Let me reclaim the process.
There are two text files; I need to open them simultaneously using two floors of loop. On the first floor of the loop, a text file will be read per line to track the beginning and end times and do other things. On the second floor of the loop, another text file will be read per line to record the text and do other things from the beginning and end times given by the first floor. The problem is: I want to open or keep the file pointer or other things when a file is opened on the second floor and reopen or locate it at the location of the last opened file on the second floor at the next loop of the first floor (anyway with the highest performance), not from the beginning of the entire file on the second floor.
Still unclear. Please show an example.
Hi Stefan,
Here is the example code of my project now.
from functools import partial
def lined_file_reader(file):
"""
Reference:https://lovemma.github.io/posts/python-%E5%A6%82%E4%BD%95%E6%B5%81%E5%BC%8F%E8%AF%BB%E5%8F%96%E5%A4%A7%E6%96%87%E4%BB%B6/
"""
for line in iter(partial(file.readline), ''):
yield line
with open('test.txt', 'r') as f:
with open('test1.txt', 'r') as f1:
for line in lined_file_reader(f):
# do something
#Read begin
f_begin='10:00:00'
#Read end
f_end='11:00:00'
for line1 in lined_file_reader(f1):
recording=False
#Record begin
f1_begin='10:00:01'
#Record end
f1_end='11:00:01'
if f1_begin>f_begin:
recording=True
if recording:
#Record text and do something
pass
if f1_end>f_end:
recording=False
break
Example rather means example data, and what you want to happen with it.
What’s the point of that? The linked original uses file.read
for good reason. Yours just seems harmful with no benefit.
Thanks anyway.
Just want to have a higher performance. If not necessary, it can be deleted.
with open('test.txt', 'r') as f:
with open('test1.txt', 'r') as f1:
for line in lined_file_reader(f):
# do something
#Read begin
f_begin='10:00:00'
#Read end
f_end='11:00:00'
#TODO: optimize this loop that doesn't begin with the beginning of the file, but from the last line of the text file read.
for line1 in lined_file_reader(f1):
recording=False
#Record begin
f1_begin='10:00:01'
#Record end
f1_end='11:00:01'
if f1_begin>f_begin:
recording=True
if recording:
#Record text and do something
pass
if f1_end>f_end:
recording=False
break
The thing gave me a headache.
Could we please see a small example of what is in each file to start, and what the result should be when the code runs? Right now, the code doesn’t look like it cares about the file contents at all, so it doesn’t help with understanding how you want to process the file.
The output stream, what it looks like now, and what it wants to be are below (note: the content marks with “*” are the same).
Results:
****-**-** **:**:55,274 [Confidentail][f]
****-**-** **:**:55,278
****-**-** **:**:55,278
****-**-** **:**:55,279
****-**-** **:**:55,279
****-**-** **:**:55,280
**/**/**** **:**:55.411835 [Confidentail][f1]
**/**/**** **:**:55.412167
**/**/**** **:**:55.412419
**/**/**** **:**:55.412577
****-**-** **:**:55,281
****-**-** **:**:55,281
****-**-** **:**:55,283
****-**-** **:**:55,283
**/**/**** **:**:55.411835
**/**/**** **:**:55.412167
**/**/**** **:**:55.412419
**/**/**** **:**:55.412577
****-**-** **:**:55,286
****-**-** **:**:55,286
****-**-** **:**:55,288
****-**-** **:**:55,288
****-**-** **:**:55,290
Expect:
****-**-** **:**:55,274 [Confidentail][f]
****-**-** **:**:55,278
****-**-** **:**:55,278
****-**-** **:**:55,279
****-**-** **:**:55,279
****-**-** **:**:55,280
**/**/**** **:**:55.411835 [Confidentail][f1]
**/**/**** **:**:55.412167
**/**/**** **:**:55.412419
**/**/**** **:**:55.412577
****-**-** **:**:55,281
****-**-** **:**:55,281
****-**-** **:**:55,283
****-**-** **:**:55,283
**/**/**** **:**:55.427811 [Confidentail][f1]
**/**/**** **:**:55.428053
**/**/**** **:**:55.428268
**/**/**** **:**:55.443808
**/**/**** **:**:55.444031
**/**/**** **:**:55.444222
****-**-** **:**:55,286
****-**-** **:**:55,286
****-**-** **:**:55,288
****-**-** **:**:55,288
****-**-** **:**:55,290