python中文件处理--判断文件读取结束方法

一、readline函数

按行遍历读取文件的方法,通过这个方法,readline() 每次只读取一行,通常比 .readlines() 慢得多。仅当没有足够内存可以一次读取整个文件时,才应该使用 .readline()

filename = raw_input('Enter your file name') #输入要遍历读取的文件路径及文件名
file = open(filename,'r')
done = 0
while not done:
aLine = file.readline()
if(aLine != ''):
print aLine,
else:
done = 1
file.close() #关闭文件

二、readlines()

readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python 的 for ... in ... 结构进行处理

fh = open('c:\autoexec.bat')
for line in fh.readlines():
print line

 

转载于:https://www.cnblogs.com/jodiezhu2019/p/10770007.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值