具有多行提要的Python文件读取行问题

具有多行提要的Python文件读取行问题,python,python-3.x,file,readline,Python,Python 3.x,File,Readline,我正试图逐行解析一个大文件。但是,当我运行此程序时: def main(): fd_in = open('file1.txt') ctr = 0 while True: line = fd_in.readline().strip() if not line: break print(line) ctr += 1 if ctr % 1000000 == 0:

我正试图逐行解析一个大文件。但是,当我运行此程序时:

def main():
    fd_in = open('file1.txt')
    ctr = 0

    while True:
        line = fd_in.readline().strip()
        if not line:
            break

        print(line)

        ctr += 1
        if ctr % 1000000 == 0:
            print(ctr)

    print(fd_in.tell())
    fd_in.close()
它在读取所有文件之前停止

[...]
495448578 # tell result
如果我在文件的错误端之前转储了8个字节:

hexdump -C -s 495448570 -n 10 file1.txt
1d87f1fa  68 65 6c 6c 6f 0d 0a 0d 0a 68                    |hello....h|
所以readline应该返回换行符,而不是空字符串

我是不是遗漏了什么


谢谢您的帮助。

如果您的
只是空白,则
条带()
会将
变为空字符串,触发该
中断
。检查eof后脱衣

def main():
fd_in=open('file1.txt')
ctr=0
尽管如此:
line=fd_in.readline()
如果不是直线:
打破
line=line.strip()
打印(行)
ctr+=1
如果ctr%1000000==0:
打印(ctr)
打印(fd_in.tell())
fd_in.close()

我完全没有注意到这一点。我的错,谢谢你的快速回复!