# -*- coding: cp936 -*-
#转载源于:http://blog.csdn.net/houyj1986/article/details/21196027
#计算文件行数
#1、文件比较小:
fobj = open(r"C:\test.txt",'rU')
len_fobj = len(fobj.readlines())
print len_fobj #2、文件比较大
len_fobj = -1
for len_fobj,line in enumerate(open(r"C:\test.txt",'rU')):
pass
len_fobj += 1
print len_fobj #3、较好的方法
import string
len_fobj = 0
fobj = open(r"C:\test.txt",'rb') while True:
buffer = fobj.read(1024 * 8192)
if not buffer:
break
len_fobj += buffer.count('\n')
fobj.close()
print len_fobj
有以下需后续确定:
1、readline与readlines的区别?
2、第二种方法中迭代语句的解释?
3、第三种方法思路:读取文件中的换行符?