In: Computer Science
In Python:
Assume a file containing a series of integers is named numbers.txt and exists on the computer's Disk. Write a program that reads al the numbers stored in the file and calculates their total.
- create your own text file called numbers.txt and put in a set of 20 numbers (each number should be between 1 and 100).
- Each number should be on its own line.
- do not assume that the file will always have 20 numbers (it may have more, it may have less).
Please Use Python.
Output:
Explanation: open() used for opening the file."r" indicates the file is opened in reading mode.
read() used for read the data from the file.
split() is used to split the data readed. If no arguments passed then it automatically takes newline as an argument.
then we calculate the sum .
finally close the file by using close()
If you want to read the data by comma seperated, just pass ' , ' as split argument. Rest of the code is same.' as split argument.