Writing a list to a file with Python

2,036,706

Solution 1

You can use a loop:

with open('your_file.txt', 'w') as f:
    for item in my_list:
        f.write("%s\n" % item)

In Python 2, you can also use

with open('your_file.txt', 'w') as f:
    for item in my_list:
        print >> f, item

If you're keen on a single function call, at least remove the square brackets [], so that the strings to be printed get made one at a time (a genexp rather than a listcomp) -- no reason to take up all the memory required to materialize the whole list of strings.

Solution 2

What are you going to do with the file? Does this file exist for humans, or other programs with clear interoperability requirements?

If you are just trying to serialize a list to disk for later use by the same python app, you should be pickleing the list.

import pickle

with open('outfile', 'wb') as fp:
    pickle.dump(itemlist, fp)

To read it back:

with open ('outfile', 'rb') as fp:
    itemlist = pickle.load(fp)

Solution 3

The simpler way is:

with open("outfile", "w") as outfile:
    outfile.write("\n".join(itemlist))

You could ensure that all items in item list are strings using a generator expression:

with open("outfile", "w") as outfile:
    outfile.write("\n".join(str(item) for item in itemlist))

Remember that all itemlist list need to be in memory, so, take care about the memory consumption.

Solution 4

Using Python 3 and Python 2.6+ syntax:

with open(filepath, 'w') as file_handler:
    for item in the_list:
        file_handler.write("{}\n".format(item))

This is platform-independent. It also terminates the final line with a newline character, which is a UNIX best practice.

Starting with Python 3.6, "{}\n".format(item) can be replaced with an f-string: f"{item}\n".

Solution 5

Yet another way. Serialize to json using simplejson (included as json in python 2.6):

>>> import simplejson
>>> f = open('output.txt', 'w')
>>> simplejson.dump([1,2,3,4], f)
>>> f.close()

If you examine output.txt:

[1, 2, 3, 4]

This is useful because the syntax is pythonic, it's human readable, and it can be read by other programs in other languages.

Share:
2,036,706
Rks Rock
Author by

Rks Rock

Updated on January 05, 2022

Comments

  • Rks Rock
    Rks Rock over 2 years

    Is this the cleanest way to write a list to a file, since writelines() doesn't insert newline characters?

    file.writelines(["%s\n" % item  for item in list])
    

    It seems like there would be a standard way...

    • SingleNegationElimination
      SingleNegationElimination over 12 years
      do note that writelines doesn't add newlines because it mirrors readlines, which also doesn't remove them.
    • Eldwin
      Eldwin about 3 years
      its between json vs pickle. read all about it - stackoverflow.com/questions/27745500/…
  • Jason Baker
    Jason Baker almost 15 years
    This isn't terribly complex, but why not just use pickle or json so that you don't have to worry about the serialization and deserialization?
  • Jason Baker
    Jason Baker almost 15 years
    Of course the first question that springs to mind is whether or not the OP needs it to end in a newline and whether or not the amount of space matters. You know what they say about premature optimizations.
  • Jason Baker
    Jason Baker almost 15 years
    One should note that this will require that the file be opened as text to be truly platform-neutral.
  • Jason Baker
    Jason Baker almost 15 years
    +1 - Why reinvent the wheel when Python has serialization built in?
  • Alex Martelli
    Alex Martelli almost 15 years
    For example because you want an output text file that can be easily read, edited, etc, with one item per line. Hardly a rare desire;-).
  • RobM
    RobM about 13 years
    A downside: This constructs the entire contents for the file in memory before writing any of them out, so peak memory usage may be high.
  • jilles de wit
    jilles de wit about 13 years
    Note from the documentation of the os module: Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms.
  • Dan
    Dan almost 13 years
    I can never get this to work. I get this error: "text = '\n'.join(namelist) + '\n' TypeError: sequence item 0: expected string, list found"
  • wolf4
    wolf4 over 12 years
    You've to ensure that all elements in 'namelist' are strings.
  • xxjjnn
    xxjjnn about 12 years
    +1 - outfile is something like: open( "save.p", "wb" ) infile is something like: open( "save.p", "rb" )
  • gwideman
    gwideman over 10 years
    This writes a string to a file, not a list (of strings) as the OP asks
  • Jorge Rodriguez
    Jorge Rodriguez over 9 years
    I found that the \n in the first one was redundant on Python 2.7/Windows
  • mozzbozz
    mozzbozz over 9 years
    If you want to add a trailing newline here, simply add a + "\n" at the end. Don't see what problem @Dave has with this.
  • TayTay
    TayTay over 7 years
    This will write an extra newline character at the end... rather than loop, you could just write thefile.write('\n'.join(thelist))
  • Harry Duong
    Harry Duong about 7 years
    isn't it only put one line in myfile.txt, something like: ['a','b','c'] instead of writing each a,b,c on each line.
  • iipr
    iipr almost 7 years
    I would add: "Be careful with the list datatype". I was getting some weird results, maybe this can help someone: thefile.write(str(item) + "\n")
  • kRazzy R
    kRazzy R over 6 years
    are lists json serializable?
  • Big Sam
    Big Sam over 6 years
    Yes indeed, they are!
  • Big Sam
    Big Sam over 6 years
    import json ; test_list = [1,2,3]; list_as_a_string = json.dumps(test_list); #list_as_a_string is now the string '[1,2,3]'
  • kRazzy R
    kRazzy R over 6 years
    I'm doing this with open ('Sp1.txt', 'a') as outfile: json.dump (sp1_segments, outfile) logger.info ("Saved sp_1 segments") ; problem is my program runs thrice, and the results from three runs are getting mashed up. is there any way to add 1-2 empty lines so that the results from each run are discernible?
  • Big Sam
    Big Sam over 6 years
    Absolutely! could you instead do json.dump(sp1_segments + "\n\n", outfile)?
  • Filippo Mazza
    Filippo Mazza about 6 years
    The problem is that the list must fit in memory. If this is not the case, line by line is indeed a possible strategy (or else going with some alternative as in stackoverflow.com/questions/7180212/…)
  • Pyd
    Pyd about 6 years
    i dont want to add "\n" for the last item, what to do? dont want if condition
  • orluke
    orluke about 6 years
    @pyd Replace the for loop with file_handler.write("\n".join(str(item) for item in the_list))
  • xlash
    xlash about 6 years
    For long list, this method is much much faster.
  • queise
    queise over 5 years
    In Python 2 use 'r' instead of 'rb' when reading the pickle if you get the "ValueError: insecure string pickle"
  • Jonathan Morales Vélez
    Jonathan Morales Vélez almost 5 years
    How do you get the file variable?
  • seralouk
    seralouk almost 5 years
    do I need to fp.close() after with open ('outfile', 'rb') as fp: ?
  • SingleNegationElimination
    SingleNegationElimination almost 5 years
    @serafeim: no; the with: block will close the file before continuing to the next statement outside of the with block.
  • Thom Ives
    Thom Ives over 4 years
    Just a little simpler for python 3.7'ish f.write(f'{item}\n')
  • Blupon
    Blupon over 4 years
    the right technique to save and load a list of lists without breaking a sweat
  • Geoff Paul Bremner
    Geoff Paul Bremner over 4 years
    the clean solution for people who don't care about any memory limitations.
  • An old man in the sea.
    An old man in the sea. about 4 years
    are 'rb' and 'wb' options? What for?
  • bballdave025
    bballdave025 over 3 years
    Executable code in relation to the comments first from @Dave , and second (from @mozzbozz) >>> with open("outfile", "w") as outfile: \n ... outfile.write("\n".join(itemlist)) \n ... outfile.write("\n") \n ... \n >>> (Written as if from the interactive Python prompt to make indentation clear. Written for my case, where lists will (mathematically) never be longer than 50.
  • luky
    luky over 3 years
    this is much better for multiline strings
  • Frederick Reynolds
    Frederick Reynolds over 3 years
    @Anoldmaninthesea. rb is to read as bytes, and wb is to write as bytes.
  • Eldwin
    Eldwin about 3 years
    thank you for adding the output, immensely helpful
  • starriet
    starriet over 2 years
    @Dave why 2x space compared to the loop?
  • starriet
    starriet over 2 years
    @RobM Aren't other answers the same? Even if we use for loop, the entire list should be loaded to the memory, no?
  • RobM
    RobM over 2 years
    @starriet: If you are writing a list to a file, the list will indeed be in memory but its string representation might not be. Consider the difference between looping through the list calling str() on every item (and storing a new list of strings) which you then iterate again to write out, versus doing it in one loop where you call str() just before writing that one item out, and then freeing the memory held by that one string.
  • starriet
    starriet over 2 years
    @RobM Ah, so for example, the selected answer consumes less memory, right? Thanks! :)
  • RobM
    RobM over 2 years
    @starriet: Yes, the main difference is going to be (peak) memory usage. See my own answer where I explore this: stackoverflow.com/a/5032851/83100
  • John Glen
    John Glen over 2 years
    It is not laziness, it is the pythonic way! Plus it works for non-strings.
  • lsm_datsci
    lsm_datsci over 2 years
    THis leaves a dangling empty line
  • titus
    titus about 2 years
    what is this doing?