Python Programming (131 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (32 Blogs)
  • Data Science (39 Blogs)
  • Mastering Python (68 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

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

Published on Mar 04,2020 5.1K Views

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. The following concepts are covered in this article:

What is Python readline()?

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

how it works - python readline-edureka

 

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.

how readline works - python readlines - edureka

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.

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. 

If you found this article on “Python readline()” relevant, check out Edureka’s Python Programming Certification Course a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

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.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.