10
Python fileinput 模块:命令行工具利器
source link: https://lotabout.me/2020/python-fileinput-module/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
命令行工具经常要处理从 stdin
或文件读取输入, fileinput
模块让我们很轻松就能实现。
示例需求
在 tail -f
看日志的时候,如果在某一行卡了很长时间,往往我们想看到底花了多长时间。因此希望有一个工具,能在每行日志前加上接收时的时间戳。例如:
$ tail -f xxx.log some good thing happend some good thing happend some bad thing happend
需要一个工具,如 timed.py
:
$ tail -f xxx.log | timed.py [2020-03-27 10:41:13.514709] some good thing happend [2020-03-27 10:41:13.525803] some good thing happend [2020-03-27 10:41:13.630232] some bad thing happend
这样就能知道花了多长时间。
示例实现
有了 fileinput
处理标准输入,只需要 4 行:
from datetime import datetime import fileinput for line in fileinput.input(): print(f'[{datetime.now()}] {line}', end='')
更多特性
其实如果只是从标准输入读取,也不麻烦,上面的例子可以写成:
from datetime import datetime import sys for line in sys.stdin: print(f'[{datetime.now()}] {line}', end='')
fileinput
同时还能处理参数( sys.args
)中的文件:
$ timed.py <file1> <file2> $ timed.py <file1> - # 读取文件 file1 后等待标准输入
对于传统的行处理程序来说,十分便利
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK