Python file program read numbers write squares

By | August 13, 2019

Task: Write a Python file program read numbers write squares. This Python programming tutorial will explain the Python program with the following statement:

Write a Python program to open an existing text file called “fnumbers.txt” on D drive. It will read each number and calculate its square. Then the program will write the square in another new text file with the name “fsquares.txt”  on D drive.

The Solution:

For the Beginners: How to Write and Run Your First Python Program in IDLE- Integrated development and Learning Envoironment for Python

First of all, open notepad software to create a new text file with numbers.

Python file program read and write files

Python file program read and write files

Python read file and write file

Python read file and write file

Write down one to seven numbers in this text file.

1
2
3
4
5
6
7

Select Save option from File menu. Write fnumbers as the name of file in file name text box. Select the file location as D drive. Click on Save button or press Enter simply.

Python file program to read numbers from a file and write squares in another

Python file program to read numbers from a file and write squares in another

Python file program to read numbers from a file and write squares in another

Python file program read and write files in Python

Write the following program in Python IDLE IDE. Save the Python program with a suitable name. Click on “Run module” command in Run menue of the Python IDLE IDE.

If the program executed successfully, you will see another file on D drive

Python file program to read numbers from a file and write squares in another

Output of read and write file program in Python

The Source code of Python file program read numbers write squares

# Write a Python program to open
# an existing file fnumbers.txt from
# D drive. The program will read numbers
# from this file and write the squares of
# every number in a new file fsquares.txt
# on D drive.

# open fnumbers.txt file in read mode
file1 = open("d:/fnumbers.txt", "r")

# create a new file fsquares.txt with "w" mode
file2 = open("d:/fsquares.txt", "w")

# use a for loop to read a number
# and calculate and write its square in
# fsquares.txt file

for n in file1:
    num = int(n)
    print(num*num, file=file2)
    
file1.close()
file2.close()

The contents of the fsquares.txt file are:

1
4
9
16
25
36
49

Since the fnumbers.txt file has the following 1 to seven numbers
1
2
3
4
5
6
7

How this program works?

  • Write a Python program to open an existing file fnumbers.txt from D drive.
  • The program will read numbers from this file
  • and write the squares of every number in a new file fsquares.txt on D drive.
  • open fnumbers.txt file in read mode
  • create a new file fsquares.txt with “w” mode
  • use a for loop to read a number
  • and calculate and write its square in fsquares.txt file
  • close the files

You may also like to read:

Python File Programs – Python File Handling Teory, 15+ File Programs in Python Programming

Write a Python file program read numbers write squares.
Python Text File Read and Show Contents Program
Python Text File Read and Display file data with Exception Handling FileNotFoundError
Python File Program Count Characters in Text File
Python Text File Program To Count Vowels

Loading

7 thoughts on “Python file program read numbers write squares

  1. Pingback: Python Text File Program To Count Vowels | EasyCodeBook.com

  2. Pingback: Python File Program Count Characters in Text File | EasyCodeBook.com

  3. Pingback: Python Exception Handling FileNotFoundError | EasyCodeBook.com

  4. Pingback: Python Text File Read and Show Contents Program | EasyCodeBook.com

  5. Pingback: Find Occurrences of Each Word in Text File Python Program | EasyCodeBook.com

  6. Pingback: Python Count Words in Text File | EasyCodeBook.com

  7. Pingback: Count Lines in Text File Python Program | EasyCodeBook.com

Leave a Reply

Your email address will not be published. Required fields are marked *