readline(limit)
limit : optional upper limit of bytes returned. 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
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()
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()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.