According to Python's Methods of File Objects, the simplest way to convert a text file into a list
is:
with open('file.txt') as f:
my_list = list(f)
# my_list = [x.rstrip() for x in f] # remove line breaks
If you just need to iterate over the text file lines, you can use:
with open('file.txt') as f:
for line in f:
...
Old answer:
Using with
and readlines()
:
with open('file.txt') as f:
lines = f.readlines()
If you don't care about closing the file, this one-liner works:
lines = open('file.txt').readlines()
The traditional way:
f = open('file.txt') # Open file on read mode
lines = f.read().splitlines() # List with stripped line-breaks
f.close() # Close file