''' commandline.py Jeff Ondich, 21 April 2009 Tianna Avery, November 2018, updated for Python 3 This program gives a brief illustration of the use of command-line arguments in Python. The program accepts a file name from the command line, opens the file, and counts the lines in the file. ''' import sys # If the user types too few or too many command-line arguments, # this code prints a usage statement and exits the program. # Note that sys.argv[0] is the name of the program, so if # I type "python commandline.py something", then sys.argv[0] # is "commandline.py" and sys.argv[1] is "something". if len(sys.argv) != 2: print('Usage:', sys.argv[0], 'filename') exit() # You don't normally need to tell the users what they just # typed, but the print statement here just verifies that # we have grabbed the right string for the file name. file_name = sys.argv[1] print('The requested file is', file_name) open_file = open(file_name) # Count the lines in the file. line_counter = 0 for line in open_file: line_counter = line_counter + 1 # Report the results. print('The file {0} contains {1} lines.'.format(file_name, line_counter)) # Clean up after yourself. open_file.close()