小编Jia*_*Gao的帖子

在Python 3和Python 2中阅读更改文件

我试图在Python中读取一个更改的文件,脚本可以处理新添加的行.我有下面的脚本打印出文件中的行,但不会终止.

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

其中'tmp.txt'由一些行组成,例如:

a
d
2
3
Run Code Online (Sandbox Code Playgroud)

如果我附加到'tmp.txt'文件,例如使用:

echo "hi" >> tmp.txt
Run Code Online (Sandbox Code Playgroud)

如果脚本使用Python 3运行,脚本将打印出新行,而不是Python 2.在Python 2中是否存在等效脚本?在这种情况下,两个版本的Python有什么不同?

python file

6
推荐指数
1
解决办法
806
查看次数

标签 统计

file ×1

python ×1