The following code reads all lines of a text file, removes the break-line characters and converts it all into a list.
For this script, i’ll assume that: the text file exists and has some content … (but you should verify this before performing the operations).
with open(filePath, "r") as textFile:
lines = textFile.readlines()
lines = list(map(lambda s: s.strip(), lines))
Using the “with” statement, it’s not necessary to explicitly close the text file. As soon as the textFile variable goes of scope, the file will be closed automatically.