
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 to read text file into a list or array with Python?
f = open('my_file.txt', 'r+') my_file_data = f.read() f.close()
The above code opens 'my_file.txt' in read mode then stores the data it reads from my_file.txt in my_file_data and closes the file. The read function reads the whole file at once. You can use the following to read the file line by line and store it in a list:
f = open('my_file', 'r+') lines = [line for line inf.readlines()] f.close()
- Related Questions & Answers
- How to read a text file in Selenium with python?
- How to read a text file with C++?
- How to read a text file in Python?
- Read integers from a text file with C++ ifstream
- How to read an entire line from a text file using Python?
- How to read a file into a string in Golang?
- How to read a number of characters from a text file using Python?
- How to read a JSON file into a DataFrame using Python Pandas library?
- Reading a text file into an Array in Node.js
- How to read a simple text file in Android App?
- How to read a text file from resources in Kotlin?
- How to read complete text file line by line using Python?
- How to work with a text file in Python?
- How to read file content into istringstream in C++?
- Read Data from a Text File using C++
Advertisements