【发布时间】:2017-03-14 20:41:27
【问题描述】:
我试图在 Python 中读取一个更改文件,其中脚本可以处理新附加的行。我有下面的脚本,它打印出文件中的行并且不会终止。
with open('tmp.txt','r') as f:
while True:
for line in f:
print(line.replace('\n',''))
其中 'tmp.txt' 由几行组成,例如:
a
d
2
3
如果我附加到'tmp.txt'文件,比如使用:
echo "hi" >> tmp.txt
如果脚本在 Python 3 上运行,但在 Python 2 上不运行,脚本将打印出新行。在 Python 2 中是否有等价物?在这种情况下,两个版本的 Python 有什么不同?
【问题讨论】: