Program to read char-by-char from file using Python

Introduction

The task is to read the file and print the words char-by-char.

Program to read char-by-char from file using Python

 

Program

ip_file = open("demo.txt", "r")   

while 1: 

    read_char = ip_file.read(1)           

    if not read_char:  

        break

#print char          

    print(read_char)   

ip_file.close()

Output

Program to read char-by-char from file using Python Output

Explanation

Approach:

  • Open file using open() function.
  • Read character using read() function. The argument to read() function is the number of characters you want to read at a time.
  • Print the character.

Author

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts

Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.