Skip to content
Related Articles

Related Articles

Python program to read character by character from a file
  • Difficulty Level : Easy
  • Last Updated : 29 Dec, 2020

Given a text file. The task is to read the text from the file character by character.

Function used:


Parameters: An integer value specified the lenght of data to be read from the file.

Return value: Returns the read bytes in form of a string.

Examples 1: Suppose the text file looks like this.


# Demonstrated Python Program
# to read file character by character
file = open('file.txt', 'r')
while 1:
    # read by character
    char =          
    if not char: 



Example 2: REading more than one charactes at a time.

# Python code to demonstrate 
# Read character by character
with open('file.txt') as f:
    while True:
        # Read from file 
        c =
        if not c:
        # print the character



 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :