‘’’
if not content 这行代码的意思是检查变量content的值是否为假(False)。
在Python中,空字符串、空列表、空字典、0等值会被视为假。因此,
if not content 的作用是检查是否已经读取了文件的末尾,如果是的话就跳出循环。
‘’’
f = r'D:\pythonXangmu\quchong\txt1.txt'
with open(f, 'r', encoding='utf-8') as file:
while True:
content = file.read(1024) # 读取1024字节
if not content:#if not content 的作用是检查是否已经读取了文件的末尾,如果是的话就跳出循环
break
print(content.strip())
'''
f = r'D:\pythonXangmu\quchong\txt1.txt'
with open(f,'r',encoding='utf-8') as file:
lines = file.readlines()
for line in lines:
print(line.strip())
f = r'D:\pythonXangmu\quchong\txt1.txt'
with open(f,'r',encoding='utf-8') as file:
lines = file.readline()
while lines :
print('while',lines.strip())
lines = file.readline()#没这句会死循环
打印效果
‘’’
1200001
1233331
1244441
0000121
1200001
1233331
1200001
1233331
1244441
0000121
1200001
1233331
‘’’