简体   繁体   English

Python没有从文件中读取最后一行

[英]Python not reading last line from file

I pull some data from DB and save it to a text file called "databaseoutput.txt".我从数据库中提取一些数据并将其保存到名为“databaseoutput.txt”的文本文件中。 When I run the code below all lines are displayed except the last one - and I know it's there because when I open the file the line that's missing when I run the code below is present当我运行下面的代码时,除了最后一行之外,所有行都显示出来 - 我知道它在那里,因为当我打开文件时,我运行下面的代码时缺少的行存在

with open("databaseoutput.txt", 'r', encoding='utf-8') as f:
    for line in f:
        print(line)

What can be causing this?什么可能导致这种情况?

I just had the same problem as you.我刚刚和你遇到了同样的问题。 When creating my text file I forgot to close it with:创建文本文件时,我忘记关闭它:

f.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM