Skip to content

Instantly share code, notes, and snippets.

@sashiyama
Last active June 30, 2018 00:33
Show Gist options
  • Save sashiyama/4428f81dd40e3e042cbaf8cd3e4f5cfe to your computer and use it in GitHub Desktop.
Save sashiyama/4428f81dd40e3e042cbaf8cd3e4f5cfe to your computer and use it in GitHub Desktop.
nl.py
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