Last active
June 30, 2018 00:33
-
-
Save sashiyama/4428f81dd40e3e042cbaf8cd3e4f5cfe to your computer and use it in GitHub Desktop.
nl.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def nl(lines): | |
i = 1 | |
for line in lines: | |
if line == '\n': | |
print("{0}".format(line), end="") | |
else: | |
print(" {0} {1}".format(i, line), end="") | |
i += 1 | |
return | |
argc = len(sys.argv) | |
if argc == 1: | |
lines = sys.stdin.readlines() | |
nl(lines) | |
elif argc == 2: | |
try: | |
f = open(sys.argv[1], 'rU') | |
lines = f.readlines() | |
nl(lines) | |
except IOError: | |
print("Error: open file", sys.stderr) | |
sys.exit(1) | |
f.close() | |
else: | |
sys.exit("usage: nl [file]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment