read numbers from a file specified on the command line and print the sum of the numbers
Question: Write a program that inputs a list of numbers from a file and prints their
sum. The file name comes in as a command-line argument. Assuming the file
"orange" contains the numbers:

    
    
    6 26 10 8 
    

Your program should behave as follows:

    
    
     python3 a4.py orange
     50
    

My current code:

    
    
    from scanner import *
    def main():
        s = Scanner("orange")
        print(sum(float(x) for x in sys.argv[1:]))
        s.close()
    main()
    

For my code to do that I will need to implement a scanner correct? However,
when I run the updated code with the scanner, it returns a `cannot convert
string to float: orange`. Sorry if this is a dumb question, but Im taking my
first programming class and I'm trying to get ahead.


Answer: You don't need a scanner, you can just get the filename from `sys.argv` and
read the file like this:

    
    
    import sys
    with open(sys.argv[1], 'r') as f:
        print(sum(float(x) for x in f.read().split()))
    

output for your sample file:

    
    
     >> python3 a4.py orange
     50.0
    

If it's guaranteed that there are only integers in your file you can use
`int(x)`.

