In this tutorial we are going to see how we can read a file and store the content of the file into a python list. While working with python many a times data is stored into text files or csv files and to use that data into our code it must be brought to the python code.
In this tutorial we will see different methods on how it can be done efficiently and with as little code as possible.
Python Read File Into List
Using with Keyword
We can use the with keyword provided by python for our job. First we need to open the file with the open() method which will take the filepath as argument and return a file descriptor to the file. We can then loop over all the lines in the file and append them one by one to our list. The code will look as below:
with open("./readfile.txt") as file : for line in file : line.strip() lines.append(line) print(lines)
Note: Watch your backslashes in windows path names, as those are also escape chars in strings. You can use forward slashes or double backslashes instead.
The strip method is only used to remove any whitespace characters like \n at the end of the lines.
There is a small problem with the above code, once opened we cannot close the file again so it is advised to first open file using a file descriptor which can be then used to close the same.
lines = [] file = open("./readfile.txt") for line in file : lines.append(line) file.close() print(lines)
The code can further shortened as:
file = open("./readfile.txt") lines = [line for line in file] file.close() print(lines)
Traditional Method of Reading File
We can use the traditional method where we will first read the file and separate the file when we encounter some special character. If we want to split the file line by line the special character will be \n. If we want to split the file word by word then we can use space as a special character. The code will look like :
file = open("./readfile.txt") lines = file.read().split('\n') file.close() print(lines)
Using readlines() Method
The readlines() method can also be used directly to read lines and store them into a list in python. The code for the same will look as below:
file = open("./readfile.txt") lines = file.readlines() file.close() print(lines)
readlines() will not remove the \n at the end of the file so if we want the \n to be removed we again need to use the strip method to get the work done.
Using splitlines() Method
The splitlines method will strip all the whitespace characters at the end of the lines and return a list containing the lines of the file.
file = open("./readfile.txt") lines = file.read().splitlines() file.close() print(lines)
Using list() Method:
We can use the list method provided by python to convert a file into list. list method will not remove the \n at the end of the file.
file = open("./readfile.txt") lines = list(file) file.close() print(lines)
Using tuple() Method
tuple can take an iterator and instantiate a tuple instance for you from the iterator that you give it. lines is a tuple created from the lines of the file. This will yield an array of lines from the file.
file = open("./readfile.txt") lines = tuple(file) file.close() print(lines)
The use of method depends on the application on which python is used. splitlines() method is most commonly used for splitting a file into lines. The split() method is considered more generic as it allows split lines using the character of our choice and not just when a new line appears.