Python program to find the sum of all numbers of file

Python program to find the sum of all numbers of a file:

In this Python tutorial, we will learn how to find the sum of all numbers found in a file. With this program, you will learn basic file handling in Python like how to read the contents of a file, how to read the characters of a file and how to check if a character is a digit or not. We will provide the path of the file to the program.

Algorithm to find the sum:

Below algorithm we will use to find the sum of all numbers in a file:

  • The file path is given. Open the file in read mode.
  • Initialize one variable to hold the sum of all numbers of the file. Initialize this variable as 0.
  • Read all contents of the file.
  • Iterate through the lines of the file one by one.
  • For each line, iterate through the characters one by one.
  • For each character, check if the character is a digit or not, if yes, add it to the sum variable. Else, ignore it.
  • Once the iteration is completed, print out the sum to the user.

Method 1: Python program to find the sum of digits of a file:

Below is the complete Python program:

given_file = open('c:\\Users\\root\\Desktop\\input.txt', 'r')

lines = given_file.readlines()
sum = 0

for line in lines:
    for c in line:
        if c.isdigit() == True:
            sum = sum + int(c)

print(sum)

given_file.close()

Download the program on Github

Here,

  • The input.txt is the file path. To run this program, you need to create one input.txt file. Make sure to update it to the file path of the file you will use in this program.
  • The open() method is used to open a file in Python. We are passing r as the second parameter. It will open the file in read mode. We are not doing any write operations to the file. So, if we open it in read mode, it will work.
  • The readlines() method returns one list holding the lines of the file. This value is assigned to the lines variable.
  • The sum variable is used to hold the sum of all digits of the file. It is initialized as 0.
  • The first line of the for loop is used to iterate through all lines of the list one by one.
  • For each line, we are iterating through the characters of that line.
  • The isdigit() method is used to check if a character is a digit or not. This method returns True if the character is a digit. It adds the digit value to the sum variable.
  • Finally, after the loops are ended, we are printing the value of sum to the user.

Sample output:

For example, if we have input.txt file with the below content:

hello1
world 23       4
new line    5
new line one more 6



7

If we run the above program, it will print 28 as the output. python sum of numbers in file

Method 2: How to use with open to open a file in Python:

We can use the with statement with the open function to open a file. The advantage of this approach is that we don’t have to close the file with the close() method explicitly. The with statement makes sure that the file is closed at the end of the program.

The following program shows how to use the with statement to find the sum:

with open('c:\\Users\\root\\Desktop\\input.txt', 'r') as given_file:
    lines = given_file.readlines()
    sum = 0

    for line in lines:
        for c in line:
            if c.isdigit() == True:
                sum = sum + int(c)

    print(sum)

Download the program on Github

If you run this program, it will give similar results.

You might also like: