python获取文件行数
代码如下:
file = r"2.txt"#引号内为文件路径 count = 1 fp = open(file, "r", encoding='utf-8') while 1: buffer = fp.read(8*1024*1024) if not buffer: break count += buffer.count('\n') print("行数:", count)
效果如下;
代码如下:
file = r"2.txt"#引号内为文件路径 count = 1 fp = open(file, "r", encoding='utf-8') while 1: buffer = fp.read(8*1024*1024) if not buffer: break count += buffer.count('\n') print("行数:", count)
效果如下;