Skip to content

Instantly share code, notes, and snippets.

@mojaveazure
Last active August 29, 2015 14:25
Show Gist options
  • Save mojaveazure/d3083f75dfca684d36ce to your computer and use it in GitHub Desktop.
Save mojaveazure/d3083f75dfca684d36ce to your computer and use it in GitHub Desktop.
A simple Python script to get the line count of a file
#!/usr/bin/env python
import sys
try:
import argparse
except ImportError:
sys.exit("Failed to import the argparse library. Please install for Python 2.6 or use Python 2.7 or higher")
Arguments = argparse.ArgumentParser(add_help=True)
Arguments.add_argument('-f',
'--file',
type=str,
default=None,
help="File to count lines")
# Count the number of lines
def line_count(name):
fname = open(name)
num_lines = sum(1 for line in fname)
fname.close()
return(num_lines)
# Do the work here
def main():
if not sys.argv[1:]:
sys.exit("Please specify a file")
else:
line_count(args.file)
# Run the program
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment