Home > Enterprise >  Reading a dynamically updated log file via readline
Reading a dynamically updated log file via readline

Time:10-22

I'm trying to read a log file, written line by line, via readline. I'm surprised to observe the following behaviour (code executed in the interpreter, but same happens when variations are executed from a file):

f = open('myfile.log')
line = readline()
while line:
  print(line)
  line = f.readline()
# --> This displays all lines the file contains so far, as expected
    
# At this point, I open the log file with a text editor (Vim),
# add a line, save and close the editor.

line = f.readline()
print(line)
# --> I'm expecting to see the new line, but this does not print anything!

Is this behaviour standard? Am I missing something?

Note: I know there are better way to deal with an updated file for instance with generators as pointed here: Reading from a frequently updated file. I'm just interested in understanding the issue with this precise use case.

CodePudding user response:

For your specific use case, the explanation is that Vim uses a write-to-temp strategy. This means that all writing operations are performed on a temporary file.

On the contrary, your scripts reads from the original file, so it does not see any change on it.


To further test, instead of Vim, you can try to directly write on the file using:

echo "Hello World" >> myfile.log

You should see the new line from python.

CodePudding user response:

for following your file, you can use this code:

f = open('myfile.log')

while True:
    line = readline()
    if not line:
        print(line)
  • Related