infile = sys.stdin
# read input
if args.input:
if args.input.endswith(".gz"):
infile = gzip.open(args.input, "r")
else:
infile open(args.input, "r")
outlines = process_lines(infile, args)
if infile != sys.stdin:
infile.close()
outfile = sys.stdout
# write output
if args.output:
if args.output.endswith(".gz"):
outfile = gzip.open(args.output, "w")
else:
outfile =
open(args.output, "w")
for line in outlines:
outfile.write("%s\n" % line)
if outfile != sys.stdout:
outfile.close()
или
def open_file(file_path: str, mode: str):
if file_path.endswith(".gz"):
return gzip.open(file_path, mode)
else:
return open(file_path, mode)
def save_result(fp, outlines):
for line in outlines:
outfile.write("%s\n" % line)
if not args.input:
outlines = process_lines(sys.stdin, args)
else:
with open_file(args.input, "r") as infile:
outlines = process_lines(args.input, args)
if not args.output:
save_result(sys.stdout, outlines)
else:
with open_file(args.output, "w") as outfile:
save_result(outfile, outlines)