- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we use file.readlines() to read multiple lines using Python?
The read function reads the whole file at once. You can use the readlines function to read the file line by line.
Example
You can use the following to read the file line by line:
f = open('my_file.txt', 'r+') for line in f.readlines(): print line f.close()
You can also use the with...open statement to open the file and read line by line. For example,
with open('my_file.txt', 'r+') as f: for line in f.readlines(): print line
- Related Articles
- How can we print multiple blank lines in python?
- How to write multiple lines in text file using Python?
- How can multiple lines be visualized using Bokeh Python?
- How do we return multiple values in Python?
- How can we read Python dictionary using C++?
- How do we use easy_install to install Python modules?
- How do we measure curved lines?
- How do we use Python in interactive mode?
- How do we use Python in script mode?
- How do we use double quotation in Python?
- How can we do Python operator overloading with multiple operands?
- How to match pattern over multiple lines in Python?
- How do we use Python Regular Expression named groups?
- How to create multiple regression lines using ggplot2 in R?
- How do we use Python regular expression to match a date string?

Advertisements