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

by | Jul 31, 2021 | Python Programs

Home » Python » Python Programs » 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

0 Comments

Submit a Comment

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.

Author