Python program to read all numbers of File

Python program to read all numbers of a file:

In this post, we will learn how to read all numbers from a file in Python and how to print all the numbers in that file. We will also learn how to find the sum of the numbers of the file. The example will use a .txt file.

Algorithm to read the numbers of a file:

Below is the algorithm we will use in the Python program:

  1. The file path is given. Open the file in read mode.
  2. Read all lines of the file.
  3. Iterate through the lines one by one.
  4. For each line, iterate through the characters of that line.
  5. For each character, we need to check if it is a digit or not. If it is a digit, print its value.

Python program:

Below is the complete Python program:

given_file = open("input.txt", "r")

lines = given_file.readlines()

for line in lines:
    for c in line:
        if c.isdigit() == True:
            print(f"Integer found : {c}")

given_file.close()

Download the code from Github

Here,

  • We are opening the file in read mode. The open() function takes the path of the file as its first argument and the mode as its second argument. We are passing r to specify that this file is opening in read mode.
  • It returns the file object. This is assigned to the given_file variable.
  • The readlines function returns a list of all the lines in that file. The for loop is used to iterate over the lines.
  • Inside the loop, it uses another for loop to iterate over the characters of each line. It uses the isdigit() function to check if a character is a digit or not. If it is a digit, it prints its value.
  • At the end of the program, we are closing the file with the close() method.

Output:

Create one file input.txt in the same folder where we have the Python file containing the above code.

If the input.txt file contains the below text :

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



7

It will print:

Integer found : 1
Integer found : 2
Integer found : 3
Integer found : 4
Integer found : 5
Integer found : 6
Integer found : 7

As you can see here, it prints all the numbers it found in the file.

python read all numbers file

Handling exception:

In the above example, if we provide any invalid path of the file or if the file is not found, it will throw a FileNotFoundError. We need to use a try..catch block to handle this error.

try:
    given_file = open("inputs.txt", "r")

    lines = given_file.readlines()

    for line in lines:
        for c in line:
            if c.isdigit() == True:
                print(f"Integer found : {c}")

    given_file.close()
except FileNotFoundError as e:
    print(e)

Download the code from Github

It will print the error as the file path inputs.txt is not correct.

[Errno 2] No such file or directory: 'inputs.txt'

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

We can find the sum of all the numbers of a file. We need to initialize one variable to hold the sum. It will be 0 at the beginning of the program. If a number is found, we will add it to the sum variable.

The below program shows how to do it programmatically in Python:

try:
    sum = 0
    given_file = open("input.txt", "r")

    lines = given_file.readlines()

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

    print(f"Sum: {sum}")
    
    given_file.close()
except FileNotFoundError as e:
    print(e)

It will print the calculated sum.

Download the code from Github

You might also like: