# # Computes the sum of all of the numbers in a file, and displays the sum # on the screen. # import sys # Read the name of the file from the user, and open that file for reading. if len(sys.argv) == 1: # There is no command line argument fname = input("Enter the name of a file that contains numbers: ") else: fname = sys.argv[1] # Get the name from the command line inf = open(fname, "r") # Initiaize a variable for the sum to 0. total = 0 # Process each line, adding its number to the sum. line = inf.readline() # Read the first line from the file while line != "": total = total + float(line) line = line.strip("\n") print(f"Adding {line} to the total which is now {total}...") line = inf.readline() # Read the next line from the file # Close the file. inf.close() # Display the sum. print(f"The sum of the values in {fname} is {total}.")