#!/usr/bin/python3 """Inhale a file and print it with percentages on the left.""" import sys def usage(retval): """Output a usage message.""" if retval: write = sys.stderr.write else: write = sys.stdout.write write('Usage: {}: -h\n'.format(sys.argv[0])) write(' Inhale stdin, output it prefixed by percentages\n') write(' We are not averaging a list.\n') write(' We are outputting the first line prepended with 0.0 and the last with nearly 100.0.\n') write(' Lines in-between get appropriate percentages to reflect how far into the file they are.\n') sys.exit(retval) def main(): """Prepend percentages: Main function.""" while sys.argv[1:]: if sys.argv[1] == '-h': usage(0) else: sys.stderr.write('{}: unrecognized option: {}\n'.format(sys.argv[0], sys.argv[1])) usage(1) del sys.argv[1] lines = sys.stdin.readlines() num_lines = len(lines) for lineno, line in enumerate(lines): try: sys.stdout.write('{:3.1f} {}'.format(lineno * 100 / num_lines, line)) except BrokenPipeError: # silently ignore broken pipe sys.exit(0) main()