What are readline() & readlines() Methods In Python

Last updated on Oct 19,2024 81.7K Views

What are readline() & readlines() Methods In Python

edureka.co

Python programming language is a pinnacle in the IT industry. With brilliant library support and out of the box features, Python programming has made file handling looking like a piece of cake. It provides easy implementations for File handling related tasks. One such method is the Python readline method. In this article, we will learn about the Python readline() method with examples.

What is Python readline()?

Python readline() method will return a line from the file when called.

 

readlines() method will return all the lines in a file in the format of a list where each element is a line in the file.

Check out Edureka’s Python programming course, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Syntax And Usage

After opening the file using the open() method, we can simply use these methods.

Python readline() syntax

file = open("filename.txt", "r")
file.readline()

The readline method takes one parameter i.e size, the default value for the size parameter is -1. It means that the method will return the whole line. It is an optional parameter, we can specify the number of bytes from a line to return.

readlines() Syntax

file = open("filename.txt","r")
file.readlines()

The readlines method takes one parameter i.e hint, the default value for the hint parameter is -1. It means that the method will return all the lines. If we specify the hint parameter, the lines exceeding the number of bytes from the hint parameter will not be returned by the readlines method.

Top 10 Trending Technologies to Learn in 2024 | Edureka

 

This video talks about the Top 10 Trending Technologies in 2024 that you must learn.
Ready to unlock the power of data? Dive into Python for Data Science today! Whether you’re analyzing trends, building predictive models, or extracting valuable insights, Python’s versatile libraries like NumPy, pandas, and scikit-learn empower you to conquer any data challenge

Let us go ahead and take a look at a few examples to understand how these methods work in Python.

Python readline() Examples

Let us suppose we have a text file with the name examples.txt with the following content.

Python is the best programming language in the world in 2020
Edureka is the biggest Ed-tech platform to learn python
Python programming is as easy as writing a program in simple English language 

Now, to use the readline, we will have to open the example.txt file.

file = open("example.txt", "r")
example1 = file.readline()
example2 = file.readline(14)
print(example1)
print(example2)

Output: Python is the best programming language in the world in 2020
         Edureka is the

readlines() Examples

We can use the readlines() method just like we used readline() in the above example.

example1 = file.readlines()
print(example1)

Output: ['Python is the best programming language in the world in 2020',
'Edureka is the biggest Ed-tech platform to learn python',
'Python programming is as easy as writing a program in simple English language']

example2 = file.readlines(80)
print(example2)

Output: ['Python is the best programming language in the world in 2020',
        'Edureka is the biggest Ed-tech platform to learn python']

This brings us to the end of this article where we have learned how we use the Python readline() method with examples. I hope you are clear with all that has been shared with you in this tutorial. 

We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Python developer. The course is designed to give you a head start into Python programming and train you for both core and advanced Python concepts along with various Python frameworks like Django.

If you come across any questions, feel free to ask all your questions in the comments section of “Python readline()”. Our team will be glad to answer.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 30th November,2024

30th November

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 28th December,2024

28th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES