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 2019-12-12 20:01  墨语i  阅读(526)  评论(0)    收藏  举报