xxxxxxxxxx
f = open(filename, "r")
listItems = f.read().splitlines()
xxxxxxxxxx
text_file = open("filename.dat", "r")
lines = text_file.readlines()
print lines
print len(lines)
text_file.close()
xxxxxxxxxx
# opening the file in read mode
my_file = open("file1.txt", "r")
# reading the file
data = my_file.read()
# replacing end of line('/n') with ' ' and
# splitting the text it further when '.' is seen.
data_into_list = data.replace('\n', ' ').split(".")
# printing the data
print(data_into_list)
my_file.close()
xxxxxxxxxx
# The best and most simple way is to use SimpleList
# Install: pip install simplelist
from simplelist import listfromtxt
from simplelist import txtfromlist
newList = listfromtxt('myFile.txt') # Make A List From A TXT File
txtfromlist('myFile.txt', myList) # Read A Python List Into A TXT File