readline(): reads a single line from a file

readline(limit)
limit : optional upper limit of bytes returned.
Output is the line of data from the file.
fob=open('data.txt','r')
print(fob.readline()) # This is first line
Output is here ( used the data.txt file given at file read() ) Using limit
fob=open('data.txt','r')
print(fob.readline(5)) # This
fob.close()
Multiple lines of data
fob=open('data.txt','r')
print(fob.readline())
print(fob.readline())
Output
This is first line

This is second line

Handling End-of-Line Characters

The readline() method returns the line including the newline character:

fob = open('data.txt', 'r')
line = fob.readline()
print(repr(line))  # Output: 'This is first line\n'
fob.close()

Use Case: Processing Large Files

When dealing with large files, readline() can be used to process them one line at a time without loading the entire file into memory:

with open('largefile.txt', 'r') as file:
    while line := file.readline():
        print(line.strip())
writable()

File Append File Write
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer