Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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.
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.
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.
Course Name | Date | Details |
---|---|---|
Python Programming Certification Course | Class Starts on 31st August,2024 31st August SAT&SUN (Weekend Batch) | View Details |
Python Programming Certification Course | Class Starts on 28th September,2024 28th September SAT&SUN (Weekend Batch) | View Details |
edureka.co