When reading files in Python, there are a few different functions you can use to extract text from a file.
The three main functions you can use to read content from a file are read(), readline() and readlines().
read() reads the entire file and returns a string, readline() reads just one line from a file, and readlines() returns a list of strings representing the lines of the file.
In the rest of this article, we will go into the details of each function and the differences between read(), readline() and readlines()
The power of programming in Python is that there are many ways you can accomplish similar actions. With this flexibility, it can be tricky to understand the differences between certain functions.
One such situation is when you are performing file input and output and want to read or write to files.
When reading files in Python, there are a few different functions you can use to extract text from a file: read(), readline() and readlines().
Let’s talk about how you can use each of these functions in Python to read text from a file.
How to Use read() to Read Entire File in Python
The Python file read() function allows us to read an entire file at once and store it in a string. Depending on the size of your file, this could make sense for you and your application.
Below is a simple example showing you how to use read() in Python.
with open("example.txt") as f:
content = f.read()
One example of a case where you would use read() is if you want to check if a string is in a file.
In this case, you read in the file and then check if a given string is in the returned text.
string = "word"
in_file = False
with open("example.txt","r") as f:
if string in f.read():
in_file = True
print(in_file)
#Output:
True
Typically though, it usually is easier to use readline() or readlines() and work with the lines, instead of the entire file.
How to Use readlines() to Read All Lines of File in Python
The next function you can use to read content from a file is the readlines() function. readlines() reads all of the lines and returns a list of strings.
Using readlines() can be useful if you are going to process the file line by line, or want to extract certain lines out of a file.
Below is a simple example showing you how to use readlines() in Python.
with open("example.txt") as f:
lines = f.readlines()
One example of where you might use readlines() is if you want to read the last N lines of a file.
To read the last N Lines of a file in Python, the easiest way is to use the readlines() function and then access the last N elements of the returned list.
n = 5
with open("example.txt") as f:
last_n_lines = f.readlines()[-n:]
One other example is if you want to remove whitespace from the lines of a file.
When reading the contents of a file, whitespace sometimes can cause us troubles. To remove whitespace from each line when using Python, you can use the Python strip() function with readlines()
myfile = open("example.txt", "r")
lines = myfile.readlines()
for line in lines:
stripped_line = line.strip()
Using readline() to Read Lines of File in Python
The last function you can use to read content from a file is the readline() function. When you open a file in Python, Python returns a generator and you can iterate over the lines with this generator.
For example, when you open a file, we are “pointing” at the first line and so when you use readline(), you can read the first line of the file.
Below shows you a simple example of how to use readline() in Python.
with open("example.txt") as f:
first_line = f.readline()
If you want to read multiple lines, then you can use readline() multiple times.
with open("example.txt") as f:
first_line = f.readline()
second_line = f.readline()
third_line = f.readline()
readline() can be useful if you are doing processing and just want to access particular line, but in the examples we have gone over here, readlines() and read() typically give you more flexibility to work with files.
Hopefully this article has been useful for you to learn about the differences between read(), readline() and readlines() in Python.