Python Program Read a File Line by Line Into a List

To understand this example, you should have the knowledge of the following Python programming topics:


Example 1: Using readlines()

Let the content of the file data_file.txt be

honda 1948
mercedes 1926
ford 1903

Source Code

Output

['honda 1948\n', 'mercedes 1926\n', 'ford 1903']
['honda 1948', 'mercedes 1926', 'ford 1903']

readlines() returns a list of lines from the file.

  • First, open the file and read the file using readlines().
  • If you want to remove the new lines ('\n'), you can use strip().

Example 2: Using for loop and list comprehension

Output

['honda 1948\n', 'mercedes 1926\n', 'ford 1903']
['honda 1948', 'mercedes 1926', 'ford 1903']

Another way to achieve the same thing is using a for loop. In each iteration, you can read each line of f object and store it in content_list as shown in the example above.