您的位置:首页 > 编程语言 > Python开发

python计算文件的行数和读取指定行的内容

2016-06-08 09:16 1051 查看
小文件读取,计算行数

1. count = len(open(filepath,'rU').readlines())

大文件读取,计算行数

2. count = -1

   for count, line in enumerate(open(thefilepath, 'rU')):

       pass

   count += 1

3. count = 0

   thefile = open(thefilepath, 'rb')

   while True:

      buffer = thefile.read(8192*1024)

      if not buffer:

         break

      count += buffer.count('\n')

   thefile.close( )

读取指定行
import linecache
count = linecache.getline(filename,linenum)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息