
👉Python readline() Method with Examples
Python readline: The readline() method in Python is a powerful file-handling method that reads one complete line from a given file. It appends a newline character (\n) at the end of the returned string.
👉Key Features of readline()
- Reads only one complete line from the given file.
- Appends a newline character (\n) at the end of the string.
- If the file is opened in normal read mode, readline() returns a string.
- If the file is opened in binary mode, readline() returns a binary object.
- The size parameter can limit the number of characters read (optional).
👉Python readline Syntax
python
file.readline(size)
👉Python readline Parameters:
- size (optional): Specifies the maximum number of characters to read. If omitted, the entire line is returned.
👉Python readline Return Value:
- Returns the string containing the line read from the file.
👉Example 1: Reading the First Line from a File
python
myfile = open(“demo.txt”, “r”)
myline = myfile.readline()
print(myline)
myfile.close()
👉Output:
nginx
Testing – FirstLine
👉Example 2: Using the size Parameter in readline()
python
myfile = open(“demo.txt”, “r”)
myline = myfile.readline(10)
print(myline)
myfile.close()
👉Output:
nginx
Testing –
👉Basic File I/O in Python
The open() function is used to open a file in Python. It accepts two main arguments:
- File path – The path to the file.
- Mode – Specifies the file mode (read, write, etc.).
👉Common Modes in open() Method:
Mode | Description |
r | Opens the file in read mode. |
w | Opens the file in write mode (overwrites content). |
a | Opens the file in append mode (adds content to the end). |
rb | Opens the file in binary read mode. |
wb | Opens the file in binary write mode. |
👉Example 3: Reading All Lines Using readline() in a Loop
python
myfile = open(“demo.txt”, “r”)
myline = myfile.readline()
while myline:
print(myline.strip()) # Removes extra newline characters
myline = myfile.readline()
myfile.close()
👉Output:
scss
Testing – FirstLine
Testing – SecondLine
Testing – Third Line
Testing – Fourth Line
Testing – Fifth Line
👉Example 4: Reading All Lines Using readlines()
The readlines() method reads all lines in a file and returns them as a list.
python
myfile = open(“test.txt”, “r”)
mylist = myfile.readlines()
print(mylist)
myfile.close()
👉Output:
css
[‘Line No 1\n’, ‘Line No 2\n’, ‘Line No 3\n’, ‘Line No 4\n’, ‘Line No 5’]
👉Example 5: Reading Lines Using for Loop
python
myfile = open(“test.txt”, “r”)
for line in myfile:
print(line.strip())
myfile.close()
👉Output:
pgsql
Line No 1
Line No 2
Line No 3
Line No 4
Line No 5
👉Example 6: Reading Lines Using while Loop
python
myfile = open(“test.txt”, “r”)
while True:
line = myfile.readline()
if not line:
break
print(line.strip())
myfile.close()
👉Output:
pgsql
Line No 1
Line No 2
Line No 3
Line No 4
Line No 5
👉Summary
Python’s readline() method is ideal for reading files line-by-line, providing better control when processing large files. Additionally, combining readline() with loops or using readlines() enhances flexibility in file handling operations.