当前位置:首页 > IT技术 > 编程语言 > 正文

Python 实现 cat
2021-10-28 15:20:47

import argparse
parser = argparse.ArgumentParser()

parser.add_argument('files', metavar='F', type=str, nargs='+')
parser.add_argument('-n', '--numbers', action='store_true',
        help='Print line numbers')

args = parser.parse_args()

print(">>> parsed args: ", args)

line_number = 1
for in_file_name in args.files:
    in_file = open(in_file_name)
    if args.numbers:
        for line in in_file.readlines():
            print(f"	{line_number}	{line}", end="")
            line_number += 1
    else:
        print(in_file.read())

 

本文摘自 :https://blog.51cto.com/u

开通会员,享受整站包年服务立即开通 >