- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
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
- How can we print multiple blank lines in python?
- How to write multiple lines in text file using Python?
- How do we return multiple values in Python?
- How can multiple lines be visualized using Bokeh Python?
- How can we read Python dictionary using C++?
- How do we use easy_install to install Python modules?
- 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 to match pattern over multiple lines in Python?
- How can we do Python operator overloading with multiple operands?
- Why do we use random.seed() in Python?
- Why do we use pandas in python?
- How do we use Python Regular Expression named groups?
- How do I split a multi-line string into multiple lines?