Last updated: Apr 10, 2024
Reading time·4 min
To read a file until a specific character:
file.read()
method to read the file's contents into a string.str.split()
method to split the file on the given character.0
.with open('example.txt', 'r', encoding='utf-8') as file: contents = file.read() character = '!' result = contents.split(character) print(result) # 👉️ ['bobby\nhadz\n.com\n', '\none\ntwo\nthree'] # bobby # hadz # .com print(result[0])
The code sample assumes that you have an example.txt
file located in the same
directory.
bobby hadz .com ! one two three
We used the with
statement to open the file in reading mode.
The statement automatically takes care of closing the file for us.
The file.read()
method is used to read the contents of the entire file into a
string.
If you work with very large files that you don't want to load in memory, scroll down to the next subheading.
str.split()
method to split the string on the given character.The str.split() method splits the string into a list of substrings using a delimiter.
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
If you only need the part of the file before the character, set the maxsplit()
argument to 1
to only split the string once.
with open('example.txt', 'r', encoding='utf-8') as file: contents = file.read() character = '!' result = contents.split(character, 1) print(result) # 👉️ ['bobby\nhadz\n.com\n', '\none\ntwo\nthree'] # bobby # hadz # .com print(result[0])
This is a bit more performant if the character is contained multiple times in the file because the string is only split on the first occurrence of the given character.
Alternatively, you can use a while
loop.
This is a three-step process:
file.read(1)
method to read the file character by character in a
while
loop.with open('example.txt', 'r', encoding='utf-8') as file: result = '' stop_char = '!' while True: char = file.read(1) if char == stop_char: break if not char: print('Reached end of file') break result += char # bobby # hadz # .com print(result)
The code sample assumes that you have an example.txt
file located in the same
directory.
bobby hadz .com ! one two three
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 check if the current character is equal to the stop character.
If the condition is met, we exit the loop, otherwise, we add the character to
the result
string.
The break statement breaks out of the innermost enclosing for or while loop.
I've also written an article on how to read a file character by character.
You can learn more about the related topics by checking out the following tutorials: