写一个类似linux head的小工具,在window下用。
head.py
# -*- coding: UTF-8 -*-
#!/usr/bin/python
# by Jakcing 2019.02.17
# print first n lines or last n lines in big file
import sys
import getopt
import linecache
VERSION="1.1"
def get_line_count(filename):
line_count = 0
file = open(filename,'r+', encoding='utf-8')
while True:
buffer = file.read(8192 * 1024 )
if not buffer:
break
line_count += buffer.count('\n')
file.close()
return line_count
def read_head_line(flist, line_num):
for file_name in flist:
print("===%s===" % file_name)
f = open(file_name, 'r', encoding='utf-8')
if(f):
for i in range(line_num):
print(