The
Python
readline()
is a
Python file handling
method. As the method name suggests, it only read one complete line from the given file. This method returns the string value along with the new line (
"\n"
), which is at the tail of every string. By default, the Python file
readline()
method returns the complete line with each call. However, we can specify the
size
parameter and get the specified number of characters from the line.
Properties of Python file readline() method
Here are some important properties of the Python read line method:
-
The
readline()
method only reads one complete line at a time. -
It adds a newline (
"\n"
) at the end of every line. - It returns a string value if the file is opened in normal read “r” mode.
- This method returns the binary object if the file is opened in the binary mode “b”.
- It accepts the size parameter that represents the number of characters that should be read from the line.
Python readline() syntax:
file.readline(size)
Parameter:
size (optional): The size parameter is an optional parameter and by default, its value is -1. which means it reads and returns the complete line. Also, the size parameter only accepts an integer value. Return Value of Python readline() Method It returns a complete line in the string format, and with each call, it reads the new line from the file.
Python readline() Method Examples
Now let’s see some examples of the
readline()
method that involves reading text lines from the example.txt file.
Example 1: Python read 2 lines from a text file
#open file with read mode
with open("example.txt", "r") as file:
print(file.readline()) #read first line
print(file.readline()) #read second line
Output
line1 Techgeekbuzz.com
line2 Techgeekbuzz.com
In the above example, you can see that there is a newline gap between
line1
and
line2
.
This is because the
readline()
method also returns the new line(
"\n"
) at the end of every return string. If you want to remove this extra line, you can use the
Python string strip()
method and remove the trailing newline.
#open file with read mod
with open("example.txt", "r") as file:
print(file.readline().strip()) #read first line
print(file.readline().strip()) #read second line
Output
line1 Techgeekbuzz.com
line2 Techgeekbuzz.com
Example 2: Print all the lines from the text file using readline() method
The
readline()
method can only read one line at a time. If you want to read all the lines from the text file, you need to use a Python loop. You can either use
Python for loop
or
Python while loop
, both will work fine.
How to read a file line by line using for loop?
#open file with read mode
with open("example.txt", "r") as file:
for line in file:
print(line.strip())
Output
line1 Techgeekbuzz.com
line2 Techgeekbuzz.com
line3 Techgeekbuzz.com
line4 Techgeekbuzz.com
line5 Techgeekbuzz.com
line6 Techgeekbuzz.com
line7 Techgeekbuzz.com
In the above example, you can see that you do not even need the
readline()
method to read all the lines from the file. The for loop iterates over the file method line by line.
How to read a file line by line using
while
loop and
readline()
method?
#open file with read mode
with open("example.txt", "r") as file:
line = file.readline()
while line:
print(line.strip())
line = file.readline()
Output
line1 Techgeekbuzz.com
line2 Techgeekbuzz.com
line3 Techgeekbuzz.com
line4 Techgeekbuzz.com
line5 Techgeekbuzz.com
line6 Techgeekbuzz.com
line7 Techgeekbuzz.com
Unlike for loop, while loop requires you to use the
readline()
method to read all the lines present in the text file.
Example 3: Size parameter of Python file
readline(size)
method
By default, the
readline()
method reads one complete line at a time and returns
"\n"
at the end of each line. Sometimes a line could be too long to fit in a display. So, we can also specify the size parameter to the
readline()
method and read the specified number of characters with each line. By specifying the size parameter, the
readline(size)
method does not read specified characters from each line, instead, it divides each line according to the specified size.
#open file with read mode
with open("example.txt", "r") as file:
print(file.readline(10).strip()) # read first 10 characters from line
print(file.readline(100).strip()) # read next 100 characters from line
Output
line1 Tech
geekbuzz.com
In this example, you can see that the
file.readline(10).strip()
statement returns the
10
characters from its line, and the statement
file.readline(100).strip(),
returns the next remaining
100
characters from the same line. Although there were not 100 characters left in that line alone, it did not read the next line. You can also print all the lines from the text file and every line would be under the
size
of the specified length.
#open file with read mode
with open("example.txt", "r") as file:
line = file.readline(15) #read only 15 chacracters
while line:
print(line.strip())
line = file.readline(15) #read only 15 characters
Output
line1 Techgeekb
uzz.com
line2 Techgeekb
uzz.com
line3 Techgeekb
uzz.com
line4 Techgeekb
uzz.com
line5 Techgeekb
uzz.com
line6 Techgeekb
uzz.com
line7 Techgeekb
uzz.com
In the above example, you can see that every line is divided into 15 characters.
Python File readlines() Method
In general, we use the Python
readlines()
method when we want to iterate over the complete content of the file line-by-line. It is ideal to use the Python file
readlines()
function with the small text files. It read the complete content of the file in one go and return a list of file lines.
Python read lines syntax:
file.readlines(hint)
Parameter:
The
readlines(hints)
method accepts the optional parameter
hint
, which is an integer value. The Hint parameter represents the number of bytes. If the number of bytes returned by the
readlines()
method exceeds the hint number, the
readlines()
will return no more lines. By default, the value of hint is -1, which signifies 'return all lines'.
Return value of readlines() method
The
readlines()
method returns a list of string lines.
Example:
#open file with read mode
with open("example.txt", "r") as file:
print(file.readlines())
Output
['line1 Techgeekbuzz.com\n', 'line2 Techgeekbuzz.com\n', 'line3 Techgeekbuzz.com\n', 'line4 Techgeekbuzz.com\n', 'line5 Techgeekbuzz.com\n', 'line6 Techgeekbuzz.com\n', 'line7 Techgeekbuzz.com']
In the above example, you can see that the
readlines()
method returns a list of lines. Also, every line has a trailing newline
"\n"
, except the last line
line7 Techgeekbuzz.com.
The above example of
readlines
can be performed using
file.read()
and
split()
methods:
#open file with read mode
with open("example.txt", "r") as file:
print(file.read().split("\n"))
Output
['line1 Techgeekbuzz.com', 'line2 Techgeekbuzz.com', 'line3 Techgeekbuzz.com', 'line4 Techgeekbuzz.com', 'line5 Techgeekbuzz.com', 'line6 Techgeekbuzz.com', 'line7 Techgeekbuzz.com']
It is important to note that with the
read()
and
split()
methods, you will not have any trailing newline
"\n"
with every string element.
Conclusion
The Python file
readline()
method or function reads the file line-by-line and returns one line at a time. It accepts an optional parameter
size
that divides each line's characters based on the size value. There is another function
readlines()
that reads all the lines of a file in one go and returns a list of lines. Both
readline()
and
readlines()
methods append a newline (
"\n"
) at the end of every line of the file. To read all the content of the file at once, you can use the read() method. Also, the
readlines()
and
readline()
methods are used to read the content line-by-line.
People are also reading:
- How to use Gmail API in python to send mail?
- Install python package using jupyter notebook
- How to extract all stored chrome password with Python
- How to automate login using selenium in Python
- Your guide for starting python coding on a MacBook
- Python counter in collections with example
- Reading and writing CSV files in python using CSV module pandas
- Python copy file and directory using shutil
- Python map() function with Examples
- How to delete emails in Python
Leave a Comment on this Post