python打印文件的前几行或最后几行教程
作者:william_djj 时间:2022-04-30 01:14:09
写一个类似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(f.readline(), end='')
def read_last_line(flist, line_num):
for file_name in flist:
linecache.clearcache()
line_count = get_line_count(file_name)
print('===%s=== line count total: %d' % (file_name, line_count))
line_count = line_count - line_num +1
print('===%s=== line %d to end' % (file_name, line_count))
for i in range(line_num):
last_line = linecache.getline(file_name, line_count) #从第line_count行读起
print('line[%d]:%s' % (line_count, last_line), end='')
line_count += 1
try:
opts, args = getopt.getopt(sys.argv[1:], "hvn:t:", ["help", "output=", "numline=", "tail=", "version"])
except getopt.GetoptError:
print("argv error,please input")
for cmd, arg in opts:
if cmd in ("-h", "--help"):
print("usage: head.py -n number filename \nor head.py -t number filename")
sys.exit()
elif cmd in ("-n", "--numline"): #打印文件开头的line_num行
line_num=int(arg)
file_names=args[:]
read_head_line(file_names, line_num)
elif cmd in ("-t", "--tail"): #打印文件结尾的line_num行
line_num=int(arg)
file_names=args[:]
read_last_line(file_names, line_num)
elif cmd in ("-v", "--version"):
print("%s %s" % (sys.argv[0], VERSION))
sys.exit()
来源:https://blog.csdn.net/william_djj/article/details/87537318
标签:python,打印,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python使用matplotlib绘制三维参数曲线操作示例
2021-03-30 05:55:31
![](https://img.aspxhome.com/file/2023/6/102776_0s.png)
ORACLE客户端连接服务器的注意事项
2007-08-17 09:57:00
如何用CocosCreator制作微信小游戏
2023-08-23 16:00:02
![](https://img.aspxhome.com/file/2023/7/56117_0s.png)
Python socket编程实例详解
2022-02-21 09:26:03
简单解析PHP程序的运行流程
2023-06-22 07:35:41
![](https://img.aspxhome.com/file/2023/1/55481_0s.gif)
Python pandas处理缺失值方法详解(dropna、drop、fillna)
2023-04-03 03:45:36
Python使用pyserial进行串口通信的实例
2022-03-07 15:30:09
如何在数据库中限制检索行数?
2010-06-22 21:04:00
scrapy自定义pipeline类实现将采集数据保存到mongodb的方法
2021-03-20 02:55:59
深入了解Python中yield from语法的使用
2022-05-18 08:24:17
简单易懂Pytorch实战实例VGG深度网络
2021-09-07 19:47:24
javascript彩虹圈效果
2011-08-05 19:10:45
Python Pandas处理CSV文件的常用技巧分享
2022-06-18 12:56:19
![](https://img.aspxhome.com/file/2023/1/99621_0s.png)
WEB2.0网页制作标准教程(9)第一个CSS布局实例
2008-02-19 19:05:00
![](https://img.aspxhome.com/file/UploadPic/20082/19/2008219191629508s.gif)
SQL"不能为新插入的行确定标识"错误的解决方法
2011-04-07 11:05:00
通过排序引导用户的行为方式
2008-05-17 09:30:00
![](https://img.aspxhome.com/file/UploadPic/20085/17/200851794651896s.gif)
MySQL Group By用法
2012-01-05 19:05:14
Python 编码规范(Google Python Style Guide)
2023-04-12 03:49:35
解读JavaScript代码 var ie = !-[1,] 最短的IE判定代码
2011-06-06 10:29:00
python中requests模块的使用方法
2021-12-27 23:16:29