1、获取文件的总行数
def get_file_lines_num(file_name):
"""获取文件的总行数"""
count = 0
thefile = open(file_name, 'r')
while True:
buffer = thefile.read(1024 * 8192)
if not buffer:
break
count += buffer.count('\n')
thefile.close()
return count
修改版:
def get_file_lines_num(file_name):
"""获取文件的总行数"""
count = 0
with open(file_name, 'r') as thefile:
buffer = thefile.read(1024 * 8192)
if not buffer:
break
count += buffer.count('\n')
return count
posted on
浙公网安备 33010602011771号