How to Read a file character by character in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Table of Contents

  1. Read a file character by character in Python
  2. Using the open() function without the with statement
  3. Adding the individual characters to a List
  4. Read a file character by character using a for loop

# Read a file character by character in Python

To read a file character by character:

  1. Open the file in reading mode.
  2. Use the file.read(1) method to read the file character by character in a while loop.
  3. Use a break statement to exit the loop at the end of the file.
main.py
with open('example.txt', 'r', encoding='utf-8') as file: while True: char = file.read(1) if not char: print('Reached end of file') break print(char)

read file character by character

The code sample assumes that you have an example.txt file located in the same directory.

example.txt
bobbyhadz.com
The code for this article is available on GitHub

We used the with statement to open the file in reading mode.

The statement automatically takes care of closing the file for us.

We used a while True loop to iterate until we reached the end of the file.

The file.read() method takes a size argument that represents the number of characters to read from the file.

If you are reading a file in binary mode, then size represents the size of bytes to be read from the file.

We set the size argument to 1 to read the file character by character.

If the end of the file has been reached, the file.read() method returns an empty string.

On each iteration, we use an if statement to check if the end of the file has been reached.

If the condition is met, we use the break statement to exit the while loop.

The break statement breaks out of the innermost enclosing for or while loop.

# Using the open() function without the with statement

An alternative approach is to use the open() function without the with statement.

main.py
file = open('example.txt', 'r', encoding='utf-8') while True: char = file.read(1) if not char: print('Reached end of file') break print(char) file.close()

using open function without with statement

The code for this article is available on GitHub

The code sample achieves the same result but uses the open() function without with.

When we use the open() function directly, we have to take care of closing the file.

The with open() statement should be your preferred approach because it takes care of closing the file even if an error occurs.

# Adding the individual characters to a List

You can use the append() method if you need to add the individual characters to a list.

main.py
with open('example.txt', 'r', encoding='utf-8') as file: characters = [] while True: char = file.read(1) if not char: print('Reached end of file') break characters.append(char) print(char) print(characters) # 👉️ ['a', 'p', 'p', 'l', 'e']
The code for this article is available on GitHub

On each iteration of the while loop, we add the current character to a list.

The list.append() method adds an item to the end of the list.

Alternatively, you can use a for loop.

# Read a file character by character using a for loop

This is a four-step process:

  1. Open the file in reading mode.
  2. Use a for loop to iterate over the file.
  3. Use a nested for loop to iterate over each line.
  4. The nested for loop will read the file character by character.
main.py
with open('example.txt', 'r', encoding='utf-8') as file: for line in file: for char in line: print(char)

The code sample assumes that you have an example.txt file located in the same directory.

example.txt
bobbyhadz.com
The code for this article is available on GitHub

We used a for loop to iterate over the collection of lines in the file.

The nested for loop is used to iterate over each line.

The char variable gets assigned each character.

When using this approach, we store an entire line in memory, which might not be suitable if your file has very long lines.

The while loop from the first example doesn't have this issue.

On the other hand, when using a nested for loop, we don't have to manually break out of the loop as it is done for us automatically.

# Using the open() function directly

Here is an equivalent example that uses the open() function directly.

main.py
file = open('example.txt', 'r', encoding='utf-8') for line in file: for char in line: print(char) file.close()
The code for this article is available on GitHub

Notice that we had to call the file.close() method to avoid memory leaks.

I've also written an article on how to read a file until a specific character.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.