首页 > 解决方案 > 为什么使用 Python 读取 .txt 文件会导致控制台出现空白行?

问题描述

我正在阅读 .txt 文件的教程的一部分。我按照说明键操作键,但我的控制台日志返回一个空行。

有谁知道可能会发生什么?

employee_file = open('employees.txt', 'r')

print(employee_file.readline())

employee_file.close()

标签: pythonfile-io

解决方案


试试这样:

with open ('employees.txt') as file :
    for line in file :
        print (line)

推荐阅读