python 中是否已经存在 EOF 字符?

Is there an already existing EOF character in python?

我需要编写 for 循环,我需要 python 中的文件结尾字符。欢迎任何帮助。

提前致谢。

read 个文件 returns 遇到 EOF 时为空字符串。

while True:
    chunk = fp.read(1)
    if chunk == '':
        break

如果您尝试输出换行符 print 将自动发送 EOF。所以你不必担心。只需在 for 循环中使用 print

如果您正在读取文件 fp.read returns None,当达到 EOF 时。

while True:
    data = fp.read()
    if not data: break