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.

Leave a comment

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