[Tutor] This is driving my crazy! My file hasn't been corrupted

Richard Dillon dillonrw at comcast.net
Sun Aug 31 18:31:25 CEST 2014


My text file has five numbers, 1-5
I don't what the problem is.
I've written the file using Word (saved as .txt ) as well as TextEdit
Python 3.4.1 on a Mac

Here's the code:

#   Richard Dillon

#   This program reads data from a .txt file and calculates a total
#   data in text file: 1,2,3,4 and 5 for a total of 15
#   error message: Non-numeric data found in the file

def main():
    
    
    total = 0.0
    
    try:
        
        infile = open('/Users/richarddillon/Desktop/numbers.txt', 'r')
                
        for line in infile:
            amount = float(line)
            total += amount

        infile.close()

        print(format(total, ',.2f'))
        
    except IOError:
        print('An error occured trying to read the file.')

    except ValueError:
        print('Non-numeric data found in the file.')

    except:
        print('An error occured.')

main()


More information about the Tutor mailing list