f = open("data.txt", "r") data = f.readlines() f.close() in python

To read lines from a file in Python, we first need to open the file using the open() function in read mode. We can then use the readlines() method to read all the lines. Finally, we need to close the file using the close() method. Here's an example code snippet:

main.py
f = open("data.txt", "r")
data = f.readlines()
f.close()
57 chars
4 lines

The above code opens the file data.txt in read mode, reads all the lines from the file and stores them in the data list, and then closes the file. We can now use the data list to process the lines of the file.

gistlibby LogSnag