By Julie Raswick

2010-07-18 22:25:01 8 Comments

How do I read every line of a file in Python and store each line as an element in a list?

I want to read the file line by line and append each line to the end of the list.


@Giovanni G. PY 2017-04-26 04:57:33

Data into list

Assume that we have a text file with our data like in the following lines,

Text file content:

line 1
line 2
line 3
  • Open the cmd in the same directory (right-click the mouse and choose cmd or PowerShell)
  • Run python and in the interpreter write:

The Python script:

>>> with open("myfile.txt", encoding="utf-8") as file:
...     x = [l.rstrip("\n") for l in file]
>>> x
['line 1','line 2','line 3']

Using append:

x = []
with open("myfile.txt") as file:
    for l in file:


>>> x = open("myfile.txt").read().splitlines()
>>> x
['line 1', 'line 2', 'line 3']


>>> x = open("myfile.txt").readlines()
>>> x
['linea 1\n', 'line 2\n', 'line 3\n']


def print_output(lines_in_textfile):
    print("lines_in_textfile =", lines_in_textfile)

y = [x.rstrip() for x in open("001.txt")]

with open('001.txt', 'r', encoding='utf-8') as file:
    file =

with open('001.txt', 'r', encoding='utf-8') as file:
    file = [x.rstrip("\n") for x in file]


lines_in_textfile = ['line 1', 'line 2', 'line 3']
lines_in_textfile = ['line 1', 'line 2', 'line 3']
lines_in_textfile = ['line 1', 'line 2', 'line 3']

@Mausy5043 2018-06-03 08:53:21

is the encoding="utf-8" required?

@Giovanni G. PY 2018-06-03 09:55:07

@Mausy5043 no, but when you read a text file, you can have some strange character (expecially in italian)

@Eric O Lebigot 2018-10-23 10:57:04

read().splitlines() is provided to you by Python: it's simply readlines() (which is probably faster, as it is less wasteful).

@craq 2020-06-04 02:50:36

@EricOLebigot from the examples shown, it looks like read().splitlines() and readlines() don't produce the same output. Are you sure they're equivalent?

@Giovanni G. PY 2020-06-04 03:59:21

If you use readlines only, you need to use the strip method to get rid of the \n in the text, so I changed the last examples using a list comprehension to have the same output in both cases. So, if you use read().readlines() you will have a "clean" item with the line and without the newline characther, otherwise, you must do what you see in the code above.

@Eric O Lebigot 2020-06-05 06:06:41

Indeed. Note that in the code above all the strip() should be rstrip("\n") or spaces around a line are deleted. Also, there is no point in doing readlines() in a list comprehension: simply iterating over the file is better, as it doesn't waste time and memory by creating an intermediate list of the lines.

@CONvid19 2015-04-20 05:53:10

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)

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 ="\n") # Create a list containing all lines
f.close() # Close file

@Felix Kling 2010-07-18 22:28:10

See Input and Ouput:

with open('filename') as f:
    lines = f.readlines()

or with stripping the newline character:

with open('filename') as f:
    lines = [line.rstrip() for line in f]

@Mark 2015-08-28 07:48:03

Better, use, which does remove newlines

@becko 2016-02-10 15:30:00

Is the second version, with for line in open(filename) safe? That is, will the file be automatically closed?

@Brad Hein 2016-03-03 19:45:38

Best to read the file one line at a time rather than reading the whole file into memory all at once. Doing so doesn't scale well with large input files. See below answer by robert.

@Josiah Yoder 2017-07-07 21:31:06

@MFARID "In text mode, the default when reading is to convert platform-specific line endings (\n on Unix, \r\n on Windows) to just \n." (For Python 3, at least). So rstrip('\r') is unneeded.

@Ramisa Anjum Aditi 2018-05-11 14:16:17

lines = [x.rstrip('\n') for x in open('data\hsf.txt','r')] If I write this way, how can I close the file after reading?

@Aaron Hall 2018-05-16 21:25:22

Yes, to the point others are making here, while it's not "best practice" to use open without the context manager (or some other guaranteed way to close it), this is not really one of those cases - when the object has no more references to it it will be garbage collected and the file closed, which should happen immediately on error or not, when the list comprehension is done processing.

@Mark Amery 2019-09-01 18:31:13

@AaronHall "when the object has no more references to it it will be garbage collected and the file closed" - this is true of CPython, but not true of PyPy. Not all Python implementations immediately destruct objects when they are no longer referenced. As such, the best practice of using with with open is relevant even in this case.

@robert 2010-07-18 22:27:26

This is more explicit than necessary, but does what you want.

with open("file.txt") as file_in:
    lines = []
    for line in file_in:

@JohannesB 2018-09-19 12:44:05

I prefer this answer since it doesn't require to load the whole file into memory (in this case it is still appended to array though, but there might be other circumstances). Certainly for big files this approach might mitigate problems.

@Elias Strehle 2018-10-04 12:48:40

Appending to an array is slow. I cannot think of a use case where this is the best solution.

@OrigamiEye 2018-12-01 15:39:57

@haccks is it better because it doesn't load the whole file to memory or is there more?

@AMC 2020-01-09 18:31:52

Note: This solution does not strip newlines.

@andrebrait 2020-05-04 20:33:39

This solution does load the whole file to memory. I don't know why people think it does not.

@João Monteiro 2020-06-10 23:15:54

@andrebrait It loads the whole file into lines[] by choice, but can just load line by line.

@jeanggi90 2019-01-16 21:30:43

In case that there are also empty lines in the document I like to read in the content and pass it through filter to prevent empty string elements

with open(myFile, "r") as f:
    excludeFileContent = list(filter(None,

@AMC 2020-01-09 18:50:28

This is unpythonic, be careful.

@Jean-Francois T. 2015-02-06 03:34:48

The simplest way to do it

A simple way is to:

  1. Read the whole file as a string
  2. Split the string line by line

In one line, that would give:

lines = open('C:/path/file.txt').read().splitlines()

However, this is quite inefficient way as this will store 2 versions of the content in memory (probably not a big issue for small files, but still). [Thanks Mark Amery].

There are 2 easier ways:

  1. Using the file as an iterator
lines = list(open('C:/path/file.txt'))
# ... or if you want to have a list without EOL characters
lines = [l.rstrip() for l in open('C:/path/file.txt')]
  1. If you are using Python 3.4 or above, better use pathlib to create a path for your file that you could use for other operations in your program:
from pathlib import Path
file_path = Path("C:/path/file.txt") 
lines = file_path.read_text().split_lines()
# ... or ... 
lines = [l.rstrip() for l in]

@Mark Amery 2019-12-29 14:12:11

This is a bad approach. For one thing, calling .read().splitlines() isn't in any way "simpler" than just calling .readlines(). For another, it's memory-inefficient; you're needlessly storing two versions of the file content (the single string returned by .read(), and the list of strings returned by splitlines()) in memory at once.

@Jean-Francois T. 2019-12-31 10:00:45

@MarkAmery True. Thanks for highlighting this. I have updated my answer.

@simhumileco 2019-03-14 14:28:53

The easiest ways to do that with some additional benefits are:

lines = list(open('filename'))


lines = tuple(open('filename'))


lines = set(open('filename'))

In the case with set, we must be remembered that we don't have the line order preserved and get rid of the duplicated lines.

Below I added an important supplement from @MarkAmery:

Since you're not calling .close on the file object nor using a with statement, in some Python implementations the file may not get closed after reading and your process will leak an open file handle.

In CPython (the normal Python implementation that most people use), this isn't a problem since the file object will get immediately garbage-collected and this will close the file, but it's nonetheless generally considered best practice to do something like:

with open('filename') as f: lines = list(f) 

to ensure that the file gets closed regardless of what Python implementation you're using.

@Mark Amery 2019-12-29 13:58:20

Since you're not calling .close on the file object nor using a with statement, in some Python implementations the file may not get closed after reading and your process will leak an open file handle. In CPython (the normal Python implementation that most people use), this isn't a problem since the file object will get immediately garbage-collected and this will close the file, but it's nonetheless generally considered best practice to do something like with open('filename') as f: lines = list(f) to ensure that the file gets closed regardless of what Python implementation you're using.

@simhumileco 2019-12-30 12:22:56

Thank you for your great comment @MarkAmery! I really appreciate it.

@AMC 2020-01-09 18:44:08

@simhumileco Why have the best (correct) solution last?

@simhumileco 2020-01-09 21:27:32

@AMC because first, I wanted to show the simplest ways and for consistency of reasoning.

@simhumileco 2020-01-09 22:22:37

Besides, I hope my answer is made so that it is short and easy to read.

@Noctis Skytower 2010-07-18 22:27:24

This will yield an "array" of lines from the file.

lines = tuple(open(filename, 'r'))

open returns a file which can be iterated over. When you iterate over a file, you get the lines from that file. 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.

@Marshall Farrier 2014-12-11 07:16:21

This is the nicest answer if you want the newline characters in there. Any way to modify it to take those out without ruining the beautiful simplicity of this version?

@Noctis Skytower 2014-12-11 13:56:12

@MarshallFarrier Try lines = open(filename).read().split('\n') instead.

@Vanuan 2015-01-03 02:21:51

does it close the file?

@Noctis Skytower 2015-01-03 13:06:37

@Vanuan Since there is no remaining reference to the file after the line is run, the destructor should automatically close the file.

@jaynp 2015-05-13 05:59:21

@NoctisSkytower I find lines = open(filename).read().splitlines() a little cleaner, and I believe it also handles DOS line endings better.

@Noctis Skytower 2015-05-13 20:53:40

@dal102 Yes, I agree with you and wish that I had knowledge of the splitlines method sooner. However, note that the newline argument of the open function is None, so universal newlines mode is enabled, and splitting on '\n' is valid in this case. Especially interesting, though, is that there is a bytes.splitlines method. This gives one the ability to emulate universal newlines mode when opening a file in binary mode. You do not actually need to open a file in text mode to easily split the file's data on line boundaries and can avoid importing the re module.

@mklement0 2015-05-22 17:21:30

This is elegant (except it's worth noting in the answer itself that the trailing \n is retained in each element), but I'm curious why you chose tuple() over list(). Based on my informal tests, list() performs slightly better (probably won't matter much). list(), unlike tuple() will return a mutable sequence (which may or may not be desired).

@Noctis Skytower 2016-01-04 16:17:04

@mklement0 Assuming a file of 1000 lines, a list takes up about 13.22% more space than a tuple. Results come from from sys import getsizeof as g; i = [None] * 1000; round((g(list(i)) / g(tuple(i)) - 1) * 100, 2). Creating a tuple takes about 4.17% more time than creating a list (with a 0.16% standard deviation). Results come from running from timeit import timeit as t; round((t('tuple(i)', 'i = [None] * 1000') / t('list(i)', 'i = [None] * 1000') - 1) * 100, 2) 30 times. My solution favors space over speed when the need for mutability is unknown.

@moldovean 2014-01-12 10:58:04

f = open("your_file.txt",'r')
out = f.readlines() # will append in the list out

Now variable out is a list (array) of what you want. You could either do:

for line in out:
    print (line)


for line in f:
    print (line)

You'll get the same results.

@Martin Thoma 2018-01-16 19:42:10

Read and write text files with Python 2 and Python 3; it works with Unicode

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Define data
lines = ['     A first string  ',
         'A Unicode sample: €',
         'German: äöüß']

# Write text file
with open('file.txt', 'w') as fp:

# Read text file
with open('file.txt', 'r') as fp:
    read_lines = fp.readlines()
    read_lines = [line.rstrip('\n') for line in read_lines]

print(lines == read_lines)

Things to notice:

  • with is a so-called context manager. It makes sure that the opened file is closed again.
  • All solutions here which simply make .strip() or .rstrip() will fail to reproduce the lines as they also strip the white space.

Common file endings


More advanced file writing/reading

For your application, the following might be important:

  • Support by other programming languages
  • Reading/writing performance
  • Compactness (file size)

See also: Comparison of data serialization formats

In case you are rather looking for a way to make configuration files, you might want to read my short article Configuration files in Python.

@SilentGhost 2010-07-18 22:28:32

with open(filename) as f:
    content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content] 

@jfs 2015-01-14 10:52:03

Don't use file.readlines() in a for-loop, a file object itself is enough: lines = [line.rstrip('\n') for line in file]

@DarkCygnus 2016-08-27 03:07:10

In the case you are working with Big Data using readlines() is not very efficient as it can result in MemoryError. In this case it is better to iterate over the file using for line in f: and working with each line variable.

@Tirtha R 2018-03-02 23:24:31

I checked the memory profile of different ways given in the answers using the procedure mentioned here. The memory usage is far better when each line is read from the file and processed, as suggested by @DevShark here. Holding all lines in a collection object is not a good idea if memory is a constraint or the file is large. The execution time is similar in both the approaches.

@Gringo Suave 2018-06-15 19:14:44

Also, .rstrip() will work slightly faster if you are stripping whitespace from the ends of lines.

@Vishal Gupta 2020-06-03 01:33:17

Oneliner: with open(filename) as f: content = [i.strip() for i in f.readlines()]

@LogicalBranch 2019-04-20 14:44:31

Here is a Python(3) helper library class that I use to simplify file I/O:

import os

# handle files using a callback method, prevents repetition
def _FileIO__file_handler(file_path, mode, callback = lambda f: None):
  f = open(file_path, mode)
    return callback(f)
  except Exception as e:
    raise IOError("Failed to %s file" % ["write to", "read from"][mode.lower() in "r rb r+".split(" ")])

class FileIO:
  # return the contents of a file
  def read(file_path, mode = "r"):
    return __file_handler(file_path, mode, lambda rf:

  # get the lines of a file
  def lines(file_path, mode = "r", filter_fn = lambda line: len(line) > 0):
    return [line for line in, mode).strip().split("\n") if filter_fn(line)]

  # create or update a file (NOTE: can also be used to replace a file's original content)
  def write(file_path, new_content, mode = "w"):
    return __file_handler(file_path, mode, lambda wf: wf.write(new_content))

  # delete a file (if it exists)
  def delete(file_path):
    return os.remove() if os.path.isfile(file_path) else None

You would then use the FileIO.lines function, like this:

file_ext_lines = FileIO.lines("./path/to/file.ext"):
for i, line in enumerate(file_ext_lines):
  print("Line {}: {}".format(i + 1, line))

Remember that the mode ("r" by default) and filter_fn (checks for empty lines by default) parameters are optional.

You could even remove the read, write and delete methods and just leave the FileIO.lines, or even turn it into a separate method called read_lines.

@Mark Amery 2019-12-29 13:27:14

Is lines = FileIO.lines(path) really enough simpler than with open(path) as f: lines = f.readlines() to justify this helper's existence? You're saving, like, 17 characters per call. (And most of the time, for performance and memory reasons, you'll want to loop over a file object directly instead of reading its lines into a list anyway, so you won't even want to use this often!) I'm often a fan of creating little utility functions, but this one feels to me like it's just needlessly creating a new way to write something that's already short and easy with the standard library gives us.

@AMC 2020-01-09 18:53:49

In addition to what @MarkAmery said, why use a class for this?

@Siddharth Satpathy 2018-12-19 01:47:12

I would try one of the below mentioned methods. The example file that I use has the name dummy.txt. You can find the file here. I presume, that the file is in the same directory as the code (you can change fpath to include the proper file name and folder path.)

In both the below mentioned examples, the list that you want is given by lst.

1.> First method:

fpath = 'dummy.txt'
with open(fpath, "r") as f: lst = [line.rstrip('\n \t') for line in f]

print lst

2.> In the second method, one can use csv.reader module from Python Standard Library:

import csv
fpath = 'dummy.txt'
with open(fpath) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter='   ')
    lst = [row[0] for row in csv_reader] 

print lst

You can use either of the two methods. Time taken for the creation of lst is almost equal in the two methods.

@Charlie Harding 2019-01-01 19:16:50

What’s the advantage of the second approach? Why invoke an additional library, which adds in edge cases (the delimiter, and quotes)?

@AMC 2020-01-09 18:52:10

What is the delimiter=' ' argument for?

@LangeHaare 2018-04-30 17:41:37

Introduced in Python 3.4, pathlib has a really convenient method for reading in text from files, as follows:

from pathlib import Path
p = Path('my_text_file')
lines = p.read_text().splitlines()

(The splitlines call is what turns it from a string containing the whole contents of the file to a list of lines in the file).

pathlib has a lot of handy conveniences in it. read_text is nice and concise, and you don't have to worry about opening and closing the file. If all you need to do with the file is read it all in in one go, it's a good choice.

@asampat3090 2015-07-20 17:33:03

You could also use the loadtxt command in NumPy. This checks for fewer conditions than genfromtxt, so it may be faster.

import numpy
data = numpy.loadtxt(filename, delimiter="\n")

@Aaron Hall 2018-05-16 20:17:51

Outline and Summary

With a filename, handling the file from a Path(filename) object, or directly with open(filename) as f, do one of the following:

  • list(fileinput.input(filename))
  • using with as f, call f.readlines()
  • list(f)
  • path.read_text().splitlines()
  • path.read_text().splitlines(keepends=True)
  • iterate over fileinput.input or f and list.append each line one at a time
  • pass f to a bound list.extend method
  • use f in a list comprehension

I explain the use-case for each below.

In Python, how do I read a file line-by-line?

This is an excellent question. First, let's create some sample data:

from pathlib import Path

File objects are lazy iterators, so just iterate over it.

filename = 'filename'
with open(filename) as f:
    for line in f:
        line # do something with the line

Alternatively, if you have multiple files, use fileinput.input, another lazy iterator. With just one file:

import fileinput

for line in fileinput.input(filename): 
    line # process the line

or for multiple files, pass it a list of filenames:

for line in fileinput.input([filename]*2): 
    line # process the line

Again, f and fileinput.input above both are/return lazy iterators. You can only use an iterator one time, so to provide functional code while avoiding verbosity I'll use the slightly more terse fileinput.input(filename) where apropos from here.

In Python, how do I read a file line-by-line into a list?

Ah but you want it in a list for some reason? I'd avoid that if possible. But if you insist... just pass the result of fileinput.input(filename) to list:


Another direct answer is to call f.readlines, which returns the contents of the file (up to an optional hint number of characters, so you could break this up into multiple lists that way).

You can get to this file object two ways. One way is to pass the filename to the open builtin:

filename = 'filename'

with open(filename) as f:

or using the new Path object from the pathlib module (which I have become quite fond of, and will use from here on):

from pathlib import Path

path = Path(filename)

with as f:

list will also consume the file iterator and return a list - a quite direct method as well:

with as f:

If you don't mind reading the entire text into memory as a single string before splitting it, you can do this as a one-liner with the Path object and the splitlines() string method. By default, splitlines removes the newlines:


If you want to keep the newlines, pass keepends=True:


I want to read the file line by line and append each line to the end of the list.

Now this is a bit silly to ask for, given that we've demonstrated the end result easily with several methods. But you might need to filter or operate on the lines as you make your list, so let's humor this request.

Using list.append would allow you to filter or operate on each line before you append it:

line_list = []
for line in fileinput.input(filename):


Using list.extend would be a bit more direct, and perhaps useful if you have a preexisting list:

line_list = []

Or more idiomatically, we could instead use a list comprehension, and map and filter inside it if desirable:

[line for line in fileinput.input(filename)]

Or even more directly, to close the circle, just pass it to list to create a new list directly without operating on the lines:



You've seen many ways to get lines from a file into a list, but I'd recommend you avoid materializing large quantities of data into a list and instead use Python's lazy iteration to process the data if possible.

That is, prefer fileinput.input or with as f.

@Daniel 2018-03-29 10:30:32

I like to use the following. Reading the lines immediately.

contents = []
for line in open(filepath, 'r').readlines():

Or using list comprehension:

contents = [line.strip() for line in open(filepath, 'r').readlines()]

@Eric O Lebigot 2018-10-23 10:58:58

There is no need for readlines(), which even incurs a memory penalty. You can simply remove it, as iterating over a (text) file gives each line in turn.

@Aran-Fey 2018-10-29 17:50:42

You should use a with statement to open (and implicitly close) the file.

@MSeifert 2018-01-16 22:33:57

To read a file into a list you need to do three things:

  • Open the file
  • Read the file
  • Store the contents as list

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.

Opening the file

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:

  • Filename
  • Mode
  • Buffering (I'll ignore this argument in this answer)

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, '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

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!
    # do stuff with f

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!

Reading the file

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:

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:

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]

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:

However if the lines end with \r\n (Windows "newlines") that .rstrip() will also take care of the \r!

Store the contents as list

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 ='\n')


with open(filename) as f:
    lst =

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!


  • Use 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:.
  • Always browse the documentation for the available functions/classes. Most of the time there's a perfect match for the task or at least one or two good ones. The obvious choice in this case would be readlines() but if you want to process the lines before storing them in the list I would recommend a simple list-comprehension.

@AMC 2020-01-09 18:40:54

The last approach is the recommended approach to open a file in Python! Why is it last, then? Won't the vast majority of people just glance at the first few lines of an answer before moving on?

@MSeifert 2020-01-09 19:14:25

@AMC I haven't put much thought into it when I wrote the answer. Do you think I should put it at the top of the answer?

@AMC 2020-01-09 19:16:44

It might be best, yeah. I also just noticed that you mention Python 2, so that could be updated, too.

@MSeifert 2020-01-09 19:28:20

Ah the question was originally tagged python-2.x. It may make sense to update it more generally. I'll see if I come to that in the next time. Thanks for your suggestions. Much appreciated!

@Abdullah Bilal 2016-09-09 09:13:08

Just use the splitlines() functions. Here is an example.

inp = "file.txt"
data = open(inp)
dat =
lst = dat.splitlines()
print lst
# print(lst) # for python 3

In the output you will have the list of lines.

@Mark Amery 2019-12-29 14:10:29

Memory-inefficient compared to using .readlines(). This puts two copies of the file content in memory at once (one as a single huge string, one as a list of lines).

@jasonleonhard 2017-08-29 23:53:59

Command line version

import os
import sys
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
filename = dname + sys.argv[1]
arr = open(filename).read().split("\n") 

Run with:

python3 input_file_name.txt

@pambda 2017-03-11 08:49:01

If you want to are faced with a very large / huge file and want to read faster (imagine you are in a Topcoder/Hackerrank coding competition), you might read a considerably bigger chunk of lines into a memory buffer at one time, rather than just iterate line by line at file level.

buffersize = 2**16
with open(path) as f: 
    while True:
        lines_buffer = f.readlines(buffersize)
        if not lines_buffer:
        for line in lines_buffer:

@Newskooler 2017-04-06 08:40:16

what does process(line) do? I get an error that there is not such variable defined. I guess something needs importing and I tried to import multiprocessing.Process, but that's not it I guess. Could you please elaborate? Thanks

@Khanal 2017-04-26 13:27:04

process(line) is a function that you need to implement to process the data. for example, instead of that line, if you use print(line), it will print each line from the lines_buffer.

@David Dehghan 2018-06-30 10:28:28

f.readlines(buffersize) returns an immutable buffer. if you want to directly read into your buffer you need to use readinto() function. I will be much faster.

@Zero 2016-03-30 15:50:32

Use this:

import pandas as pd
data = pd.read_csv(filename) # You can also add parameters such as header, sep, etc.
array = data.values

data is a dataframe type, and uses values to get ndarray. You can also get a list by using array.tolist().

@AMC 2020-01-09 18:51:26

pandas.read_csv() is for reading CSV data, how is it appropriate here?

@atomh33ls 2013-06-18 10:17:33

Another option is numpy.genfromtxt, for example:

import numpy as np
data = np.genfromtxt("yourfile.dat",delimiter="\n")

This will make data a NumPy array with as many rows as are in your file.

@DevShark 2016-02-25 09:13:38

You could simply do the following, as has been suggested:

with open('/your/path/file') as f:
    my_lines = f.readlines()

Note that this approach has 2 downsides:

1) You store all the lines in memory. In the general case, this is a very bad idea. The file could be very large, and you could run out of memory. Even if it's not large, it is simply a waste of memory.

2) This does not allow processing of each line as you read them. So if you process your lines after this, it is not efficient (requires two passes rather than one).

A better approach for the general case would be the following:

with open('/your/path/file') as f:
    for line in f:

Where you define your process function any way you want. For example:

def process(line):
    if 'save the world' in line.lower():

(The implementation of the Superman class is left as an exercise for you).

This will work nicely for any file size and you go through your file in just 1 pass. This is typically how generic parsers will work.

@Ephexx 2016-05-17 21:37:08

This was exactly what I needed - and thanks for explaining the downsides. As a beginner in Python, it's awesome to understand why a solution is the solution. Cheers!

@Corey Goldberg 2016-12-12 23:19:31

the question doesn't state the need to process every line, so this answer gives irrelevant information

@DevShark 2016-12-13 07:31:26

Think a bit more Corey. Do you really ever want your computer to read each line, without ever doing anything with these lines? Surely you can realize you always need to process them one way or another.

@Corey Goldberg 2016-12-13 15:06:44

@DevShark always? that's just false.

@DevShark 2016-12-14 10:22:36

You always need to do something with the lines. It can be as simple as printing the lines, or counting them. There is no value in having your process read the lines in memory, but not doing anything with it.

@Corey Goldberg 2017-06-23 16:55:24

of course you don't always need to process items as you read them from a file at the moment you read them... that's nonsense. Perhaps you need to generate a list of items stored in a file as input to another function? Is that such an outrageous idea?

@DevShark 2017-06-23 19:40:36

You always need to do something with them. I think the point you are trying to make is that you might want to apply a function to all of them at once, rather than one by one. That is indeed the case sometimes. But it is very inefficient from a memory standpoint to do so, and prevents you from reading files if its footprint is larger than your Ram. That's why typically generic parsers operate in the way I described.

@Pierre Monico 2017-08-10 11:56:24

Good approach, but just to be precise: in this context, "processing the lines" won't alter them in the original file. You need to copy them to another file if you need them to be modified and stored.

@DevShark 2017-09-14 09:17:32

@PierreOcinom that is correct. Given that the file is opened in read only mode, you couldn't modify the original file with the code above. To open a file for both reading and writing, use open('file_path', 'r+')

@Tirtha R 2018-03-02 23:15:12

I checked the memory profile of both the ways using the procedure mentioned here. The memory usage is far better when each line is read from the file and processed, as suggested by @DevShark. Holding all lines in a collection object is not a good idea if memory is a constraint or the file is large. The execution time is similar in both the approaches.

@DevShark 2018-07-24 11:50:06

Thanks for running the numbers. This is what was expected.

@JeramieH 2018-09-12 19:01:23

@DevShark Loading the lines into a set to use as a filter list during execution. They must all be loaded into RAM, and there's no per-line processing necessary.

@DevShark 2018-09-12 19:45:36

My statement was “you always need to do something with the lines”, and your example illustrate it: you add them to a set. I think your point is that the code I wrote is not the only way to do things. That is correct. To load them all into a set, the other approach of reading all the lines in one go might be more to your liking.

@Johnny 2014-12-20 18:31:22

Clean and Pythonic Way of Reading the Lines of a File Into a List

First and foremost, you should focus on opening your file and reading its contents in an efficient and pythonic way. Here is an example of the way I personally DO NOT prefer:

infile = open('my_file.txt', 'r')  # Open the file for reading.

data =  # Read the contents of the file.

infile.close()  # Close the file since we're done using it.

Instead, I prefer the below method of opening files for both reading and writing as it is very clean, and does not require an extra step of closing the file once you are done using it. In the statement below, we're opening the file for reading, and assigning it to the variable 'infile.' Once the code within this statement has finished running, the file will be automatically closed.

# Open the file for reading.
with open('my_file.txt', 'r') as infile:

    data =  # Read the contents of the file into memory.

Now we need to focus on bringing this data into a Python List because they are iterable, efficient, and flexible. In your case, the desired goal is to bring each line of the text file into a separate element. To accomplish this, we will use the splitlines() method as follows:

# Return a list of the lines, breaking at line boundaries.
my_list = data.splitlines()

The Final Product:

# Open the file for reading.
with open('my_file.txt', 'r') as infile:

    data =  # Read the contents of the file into memory.

# Return a list of the lines, breaking at line boundaries.
my_list = data.splitlines()

Testing Our Code:

  • Contents of the text file:
     A fost odatã ca-n povesti,
     A fost ca niciodatã,
     Din rude mãri împãrãtesti,
     O prea frumoasã fatã.
  • Print statements for testing purposes:
    print my_list  # Print the list.

    # Print each line in the list.
    for line in my_list:
        print line

    # Print the fourth element in this list.
    print my_list[3]
  • Output (different-looking because of unicode characters):
     ['A fost odat\xc3\xa3 ca-n povesti,', 'A fost ca niciodat\xc3\xa3,',
     'Din rude m\xc3\xa3ri \xc3\xaemp\xc3\xa3r\xc3\xa3testi,', 'O prea
     frumoas\xc3\xa3 fat\xc3\xa3.']

     A fost odatã ca-n povesti, A fost ca niciodatã, Din rude mãri
     împãrãtesti, O prea frumoasã fatã.

     O prea frumoasã fatã.

@user1833244 2014-05-27 12:21:01

Here's one more option by using list comprehensions on files;

lines = [line.rstrip() for line in open('file.txt')]

This should be more efficient way as the most of the work is done inside the Python interpreter.

@mklement0 2015-05-22 16:39:01

rstrip() potentially strips all trailing whitespace, not just the \n; use .rstrip('\n').

@Mark Amery 2019-12-29 14:30:18

This also doesn't guarantee that the file will be closed after reading in all Python implementations (although in CPython, the main Python implementation, it will be).

@AMC 2020-01-09 18:41:52

This should be more efficient way as the most of the work is done inside the Python interpreter. What does that mean?

@Eneko Alonso 2014-03-02 04:22:39

If you want the \n included:

with open(fname) as f:
    content = f.readlines()

If you do not want \n included:

with open(fname) as f:
    content =

@oliland 2013-11-22 14:57:48

If you'd like to read a file from the command line or from stdin, you can also use the fileinput module:

import fileinput

content = []
for line in fileinput.input():


Pass files to it like so:

$ python textfile.txt 

Read more here:

Related Questions

Sponsored Content

28 Answered Questions

[SOLVED] How do I concatenate two lists in Python?

61 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

12 Answered Questions

[SOLVED] Getting the last element of a list

  • 2009-05-30 19:28:53
  • Janusz
  • 2160086 View
  • 2099 Score
  • 12 Answer
  • Tags:   python list indexing

41 Answered Questions

[SOLVED] What does the "yield" keyword do?

30 Answered Questions

[SOLVED] Finding the index of an item in a list

  • 2008-10-07 01:39:38
  • Eugene M
  • 3953916 View
  • 3241 Score
  • 30 Answer
  • Tags:   python list indexing

39 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

16 Answered Questions

[SOLVED] How do I copy a file in Python?

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

21 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 4461759 View
  • 3472 Score
  • 21 Answer
  • Tags:   python directory

34 Answered Questions

[SOLVED] How do I sort a dictionary by value?

Sponsored Content