Skip to content

Instantly share code, notes, and snippets.

@yehudalev
Forked from niitsken/nl.py
Created June 26, 2016 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yehudalev/e554474759551df9f42536f029e2581f to your computer and use it in GitHub Desktop.
Save yehudalev/e554474759551df9f42536f029e2581f to your computer and use it in GitHub Desktop.
import sys
args = len(sys.argv)
if args == 1:
print("Enter a file name below")
f = sys.stdin.readline().rstrip()
elif args == 2:
f = sys.argv[1]
else:
sys.exit("please enter the command this way: 'python nl.py [a file]'")
try:
f = open(f, "rU")
except IOError:
sys.exit("%s: No such file or directory" % (f))
i = 1
for s in f:
s = s.rstrip()
if s:
print "%5d\t%s" % (i, s)
i += 1
else:
print
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment