Last updated: Apr 10, 2024
Reading time·4 min
To read a file character by character:
file.read(1)
method to read the file character by character in a
while
loop.break
statement to exit the loop at the end of the file.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)
The code sample assumes that you have an example.txt
file located in the same
directory.
bobbyhadz.com
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.
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.
with
statementAn alternative approach is to use the
open()
function without the with
statement.
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()
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.
You can use the append()
method if you need to add the individual characters
to a list.
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']
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.
This is a four-step process:
for
loop to iterate over the file.for
loop to iterate over each line.for
loop will read the file character by character.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.
bobbyhadz.com
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.
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.
Here is an equivalent example that uses the open()
function directly.
file = open('example.txt', 'r', encoding='utf-8') for line in file: for char in line: print(char) file.close()
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.
You can learn more about the related topics by checking out the following tutorials: