# Compute the sum of all of the numbers in a file. Each number will appear # on its own line. import sys # Retrieve the file name from the command line if len(sys.argv) == 2: filename = sys.argv[1] else: # Read the file name from the user filename = input("Enter the name of a file containing some numbers: ") # Open the file inf = open(filename, "r") # Read the first line from the file and initialize total line = inf.readline() total = 0 # Keep looping until we reach the end of the file while line != "": # Remove the newline character from the end of the line line = line.rstrip() # Display the number we are about to add to the total and then add it print("About to add", line, "into the total...") total = total + float(line) # Read the next line from the file line = inf.readline() # Close the file inf.close() # Display the total print("The total of all the number is", total)