Python-按行遍历文件

Python 按行遍历文件的两种写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def readline_1():
with open(expanduser('some.txt'), 'r') as file:
line: str = file.readline()
print('readline_1:', type(line))
while line:
print('readline_1:', type(line))
line = file.readline()

def readline_2():
# https://www.guru99.com/python-file-readline.html
with open(expanduser('some.txt'), 'r') as logfile:
while True:
line: str = logfile.readline()
if line == '':
break
elif line:
print('readline_2:', type(line))

Python-按行遍历文件
https://dnacore.github.io/post/1d2464cf-e212-4b68-9b9c-0efce5b555ac.html
作者
DNACore
发布于
2025年4月26日
更新于
2025年4月26日
许可协议