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.
Latest posts by Breno RdV (see all)
- Unknown error when invoking pbwsemit.exe. (PowerBuilder) - October 3, 2018
- [Solved] Error “Unhandled Access Violation Reading” (AutoCad) - August 15, 2018
- Disable Adobe Application Manager for good - November 3, 2017