Python Program to Read Character by Character from a File (original) (raw)

Last Updated : 31 Oct, 2025

In this article, we will demonstrate how to read a file character by character using practical examples.

**Example:

**Input: Geeks
**Output: G
e
e
k
s

**Explanation: Iterated through character by character from the input as shown in the output.

**Below is the sample text file:

pythonfile-input1

Read character by character from a file

In this approach, we read one character at a time using the read(1) method, which is useful when processing files where each character needs to be analyzed individually, such as parsing or text analysis tasks.

Python `

file = open('file.txt', 'r') while 1: char = file.read(1)
if not char: break print(char) file.close()

`

**Output

python-read-character

**Explanation:

Reading more than one character

This method reads a fixed number of characters (e.g., 5) at a time using read(n), which helps improve efficiency when working with larger files while still maintaining controlled reading.

Python `

with open('file.txt') as f:

while True:
    c = f.read(5)
    if not c:
        break

    print(c)

`