Reading changing file in Python 3 and Python 2

老子叫甜甜 提交于 2021-02-07 06:56:59

问题


I was trying to read a changing file in Python, where a script can process newly appended lines. I have the script below which prints out the lines in a file and does not terminate.

with open('tmp.txt','r') as f:
    while True:
        for line in f:
            print(line.replace('\n',''))

Where 'tmp.txt' consists of some lines, e.g.:

a
d
2
3

If I appended to the 'tmp.txt' file, such as using:

echo "hi" >> tmp.txt

The script will print out the new line in if the script is run with Python 3, but not with Python 2. Is there an equivalent in Python 2? And what's different between the two versions of Python in this case?


回答1:


Looking at the objects f in python 2.7 vs 3.5 they are slightly different

The following

with open('tmp.txt','r') as f:
    print(f)
    print(type(f))

In python 2.7 returns

<open file 'tmp.txt', mode 'r' at 0x0000000003DD9780>
<type 'file'>

Whereas in python 3.5 returns

<_io.TextIOWrapper name='tmp.txt' mode='r' encoding='cp1252'>
<class '_io.TextIOWrapper'>

The same behavior can be obtained in python 2.7 using

import io

with io.open('tmp.txt','r') as f:
    while True:
        for line in f:
            print(line.replace('\n',''))



回答2:


This should do the trick.

import time

def follow(file):
    thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

if __name__ == '__main__':
    logfile = open("log.txt","r")
    loglines = follow(logfile)
    for line in loglines:
        print(line)

Found original here: Reading from a frequently updated file



来源:https://stackoverflow.com/questions/40364643/reading-changing-file-in-python-3-and-python-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!