To read a file into a list you need to do three things:
Fortunately Python makes it very easy to do these things so the shortest way to read a file into a list is:
lst = list(open(filename))
However I'll add some more explanation.
I assume that you want to open a specific file and you don't deal directly with a file-handle (or a file-like-handle). The most commonly used function to open a file in Python is open
, it takes one mandatory argument and two optional ones in Python 2.7:
The filename should be a string that represents the path to the file. For example:
open('afile') # opens the file named afile in the current working directory
open('adir/afile') # relative path (relative to the current working directory)
open('C:/users/aname/afile') # absolute path (windows)
open('/usr/local/afile') # absolute path (linux)
Note that the file extension needs to be specified. This is especially important for Windows users because file extensions like .txt
or .doc
, etc. are hidden by default when viewed in the explorer.
The second argument is the mode
, it's r
by default which means "read-only". That's exactly what you need in your case.
But in case you actually want to create a file and/or write to a file you'll need a different argument here. There is an excellent answer if you want an overview.
For reading a file you can omit the mode
or pass it in explicitly:
open(filename)
open(filename, 'r')
Both will open the file in read-only mode. In case you want to read in a binary file on Windows you need to use the mode rb
:
open(filename, 'rb')
On other platforms the 'b'
(binary mode) is simply ignored.
Now that I've shown how to open
the file, let's talk about the fact that you always need to close
it again. Otherwise it will keep an open file-handle to the file until the process exits (or Python garbages the file-handle).
While you could use:
f = open(filename)
# ... do stuff with f
f.close()
That will fail to close the file when something between open
and close
throws an exception. You could avoid that by using a try
and finally
:
f = open(filename)
# nothing in between!
try:
# do stuff with f
finally:
f.close()
However Python provides context managers that have a prettier syntax (but for open
it's almost identical to the try
and finally
above):
with open(filename) as f:
# do stuff with f
# The file is always closed after the with-scope ends.
The last approach is the recommended approach to open a file in Python!
Okay, you've opened the file, now how to read it?
The open
function returns a file
object and it supports Pythons iteration protocol. Each iteration will give you a line:
with open(filename) as f:
for line in f:
print(line)
This will print each line of the file. Note however that each line will contain a newline character \n
at the end (you might want to check if your Python is built with universal newlines support - otherwise you could also have \r\n
on Windows or \r
on Mac as newlines). If you don't want that you can could simply remove the last character (or the last two characters on Windows):
with open(filename) as f:
for line in f:
print(line[:-1])
But the last line doesn't necessarily has a trailing newline, so one shouldn't use that. One could check if it ends with a trailing newline and if so remove it:
with open(filename) as f:
for line in f:
if line.endswith('\n'):
line = line[:-1]
print(line)
But you could simply remove all whitespaces (including the \n
character) from the end of the string, this will also remove all other trailing whitespaces so you have to be careful if these are important:
with open(filename) as f:
for line in f:
print(f.rstrip())
However if the lines end with \r\n
(Windows "newlines") that .rstrip()
will also take care of the \r
!
Now that you know how to open the file and read it, it's time to store the contents in a list. The simplest option would be to use the list
function:
with open(filename) as f:
lst = list(f)
In case you want to strip the trailing newlines you could use a list comprehension instead:
with open(filename) as f:
lst = [line.rstrip() for line in f]
Or even simpler: The .readlines()
method of the file
object by default returns a list
of the lines:
with open(filename) as f:
lst = f.readlines()
This will also include the trailing newline characters, if you don't want them I would recommend the [line.rstrip() for line in f]
approach because it avoids keeping two lists containing all the lines in memory.
There's an additional option to get the desired output, however it's rather "suboptimal": read
the complete file in a string and then split on newlines:
with open(filename) as f:
lst = f.read().split('\n')
or:
with open(filename) as f:
lst = f.read().splitlines()
These take care of the trailing newlines automatically because the split
character isn't included. However they are not ideal because you keep the file as string and as a list of lines in memory!
with open(...) as f
when opening files because you don't need to take care of closing the file yourself and it closes the file even if some exception happens.file
objects support the iteration protocol so reading a file line-by-line is as simple as for line in the_file_object:
.readlines()
but if you want to process the lines before storing them in the list I would recommend a simple list-comprehension.