- Writing a list to a file with Python, with newlines
- 26 Answers 26
- Writing List to a File in Python
- Table of contents
- Steps to Write List to a File in Python
- Example to Write List to a File in Python
- Example to Read List from a File in Python
- Write a list to file without using a loop
- Pickle module to write (serialize) list into a file
- Json module to write list into a JSON file
- writelines() method to write a list of lines to a file
- About Vishal
- Related Tutorial Topics:
- Python Exercises and Quizzes
Writing a list to a file with Python, with newlines
do note that writelines doesn’t add newlines because it mirrors readlines , which also doesn’t remove them.
26 Answers 26
with open('your_file.txt', 'w') as f: for line in lines: f.write(f"\n")
with open('your_file.txt', 'w') as f: for line in lines: f.write("%s\n" % line)
For Python 2, one may also use:
with open('your_file.txt', 'w') as f: for line in lines: print >> f, line
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.
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)
with open ('outfile', 'rb') as fp: itemlist = pickle.load(fp)
«Warning: The pickle module is not secure. Only unpickle data you trust. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling. Never unpickle data that could have come from an untrusted source, or that could have been tampered with.» — From the same manual page linked on the answer.
with open("outfile", "w") as outfile: outfile.write("\n".join(itemlist))
To ensure that all items in the item list are strings, use a generator expression:
with open("outfile", "w") as outfile: outfile.write("\n".join(str(item) for item in itemlist))
Remember that itemlist takes up memory, so take care about the memory consumption.
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»\n» .
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()
This is useful because the syntax is pythonic, it’s human readable, and it can be read by other programs in other languages.
I thought it would be interesting to explore the benefits of using a genexp, so here’s my take.
The example in the question uses square brackets to create a temporary list, and so is equivalent to:
file.writelines( list( "%s\n" % item for item in list ) )
Which needlessly constructs a temporary list of all the lines that will be written out, this may consume significant amounts of memory depending on the size of your list and how verbose the output of str(item) is.
Drop the square brackets (equivalent to removing the wrapping list() call above) will instead pass a temporary generator to file.writelines() :
file.writelines( "%s\n" % item for item in list )
This generator will create newline-terminated representation of your item objects on-demand (i.e. as they are written out). This is nice for a couple of reasons:
- Memory overheads are small, even for very large lists
- If str(item) is slow there’s visible progress in the file as each item is processed
This avoids memory issues, such as:
In [1]: import os In [2]: f = file(os.devnull, "w") In [3]: %timeit f.writelines( "%s\n" % item for item in xrange(2**20) ) 1 loops, best of 3: 385 ms per loop In [4]: %timeit f.writelines( ["%s\n" % item for item in xrange(2**20)] ) ERROR: Internal Python error in the inspect module. Below is the traceback from this internal error. Traceback (most recent call last): . MemoryError
(I triggered this error by limiting Python’s max. virtual memory to ~100MB with ulimit -v 102400 ).
Putting memory usage to one side, this method isn’t actually any faster than the original:
In [4]: %timeit f.writelines( "%s\n" % item for item in xrange(2**20) ) 1 loops, best of 3: 370 ms per loop In [5]: %timeit f.writelines( ["%s\n" % item for item in xrange(2**20)] ) 1 loops, best of 3: 360 ms per loop
Writing List to a File in Python
In this article, you’ll learn how to write a list to a file in Python.
Often, we require storing a list, dictionary, or any in-memory data structure to persistent storage such as file or database so that we can reuse it whenever needed. For example, after analyzing data, you can store it in a file, and for the next time, that data can be read to use in an application.
There are multiple ways to write a Python list to a file. After reading this article, You’ll learn:
- Write a list to a text file and read it in a Python program when required using a write() and read() method.
- How to use Python’s pickle module to serialize and deserialize a list into a file. Serialize means saving a list object to a file. Deserialize means reading that list back into memory.
- Use of built-in json module to write a list to a json file.
Table of contents
Steps to Write List to a File in Python
Python offers the write() method to write text into a file and the read() method to read a file. The below steps show how to save Python list line by line into a text file.
- Open file in write mode Pass file path and access mode w to the open() function. The access mode opens a file in write mode.
For example, fp= open(r’File_Path’, ‘w’) . - Iterate list using a for loop Use for loop to iterate each item from a list. We iterate the list so that we can write each item of a list into a file.
- Write current item into the file In each loop iteration, we get the current item from the list. Use the write(‘text’) method to write the current item to a file and move to the next iteration. we will repeat this step till the last item of a list.
- Close file after completing the write operation When we complete writing a list to a file, we need to ensure that the file will be closed properly. Use file close() method to close a file.
Example to Write List to a File in Python
# list of names names = ['Jessa', 'Eric', 'Bob'] # open file in write mode with open(r'E:/demos/files_demos/account/sales.txt', 'w') as fp: for item in names: # write each item on a new line fp.write("%s\n" % item) print('Done')
Note: We used the \n in write() method to break the lines to write each item on a new line.
Below content got written in a file.
Example to Read List from a File in Python
Now, Let’s see how to read the same list from a file back into memory.
# empty list to read list from a file names = [] # open file and read the content in a list with open(r'E:\demos\files_demos\account\sales.txt', 'r') as fp: for line in fp: # remove linebreak from a current name # linebreak is the last character of each line x = line[:-1] # add current item to the list names.append(x) # display list print(names)
Write a list to file without using a loop
In this example, we are using a join method of a str class to add a newline after each item of a list and write it to a file.
The join() method will join all items in a list into a string, using a \n character as separator (which will add a new line after each list item).
# list of names names = ['Jessa', 'Eric', 'Bob'] with open(r'E:/demos/files_demos/account/sales.txt', 'w') as fp: fp.write('\n'.join(names))
If you want to convert all items of a list to a string when writing then use the generator expression.
# list of names names = ['Jessa', 'Eric', 'Bob'] with open(r'E:/demos/files_demos/account/sales.txt', 'w') as fp: fp.write("\n".join(str(item) for item in names))
Pickle module to write (serialize) list into a file
Python pickle module is used for serializing and de-serializing a Python object. For example, we can convert any Python objects such as list, dict into a character stream using pickling. This character stream contains all the information necessary to reconstruct the object in the future.
Any object in Python can be pickled and saved in persistent storage such as database and file for later use.
Example: Pickle and write Python list into a file
- First import the pickle module
- To write a list to a binary file, use the access mode ‘b’ to open a file. For writing, it will be wb , and for reading, it will be rb . The file open() function will check if the file already exists and, if not, will create one. If a file already exists, it gets truncated, and new content will be added at the start of a file.
- Next, The pickle’s dump(list, file_object) method converts an in-memory Python object into a bytes string and writes it to a binary file.
# Python program to store list to file using pickle module import pickle # write list to binary file def write_list(a_list): # store list in binary file so 'wb' mode with open('listfile', 'wb') as fp: pickle.dump(names, fp) print('Done writing list into a binary file') # Read list to memory def read_list(): # for reading also binary mode is important with open('sampleList', 'rb') as fp: n_list = pickle.load(fp) return n_list # list of names names = ['Jessa', 'Eric', 'Bob'] write_list(names) r_names = read_list() print('List is', r_names)
Done writing list into a binary file List is ['Jessa', 'Eric', 'Bob']
Json module to write list into a JSON file
We can use it in the following cases.
- Most of the time, when you execute a GET request, you receive a response in JSON format, and you can store JSON response in a file for future use or for an underlying system to use.
- For example, you have data in a list, and you want to encode and store it in a file in the form of JSON.
In this example, we are going to use the Python json module to convert the list into JSON format and write it into a file using a json dump() method.
# Python program to store list to JSON file import json def write_list(a_list): print("Started writing list data into a json file") with open("names.json", "w") as fp: json.dump(a_list, fp) print("Done writing JSON data into .json file") # Read list to memory def read_list(): # for reading also binary mode is important with open('names.json', 'rb') as fp: n_list = json.load(fp) return n_list # assume you have the following list names = ['Jessa', 'Eric', 'Bob'] write_list(names) r_names = read_list() print('List is', r_names)
Started writing list data into a json file Done writing JSON data into .json file List is ['Jessa', 'Eric', 'Bob']
writelines() method to write a list of lines to a file
We can write multiple lines at once using the writelines() method. For example, we can pass a list of strings to add to the file. Use this method when you want to write a list into a file.
# list of names names = ['Jessa', 'Eric', 'Bob'] with open(r'E:/demos/files_demos/account/sales.txt', 'w') as fp: fp.writelines(names)
Here is the output we are getting
cat sales.txt JessaEricBob
As you can see in the output, the file writelines() method doesn’t add any line separators after each list item.
To overcome this limitation, we can use list comprehension to add the new line character after each element in the list and then pass the list to the writelines method.
# list of names names = ['Jessa', 'Eric', 'Bob'] # add '\n' after each item of a list n_names = ["<>\n".format(i) for i in names] with open(r'E:/demos/files_demos/account/sales.txt', 'w') as fp: fp.writelines(n_names)
cat sales.txt Jessa Eric Bob
Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.
About Vishal
I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter
Related Tutorial Topics:
Python Exercises and Quizzes
Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.
- 15+ Topic-specific Exercises and Quizzes
- Each Exercise contains 10 questions
- Each Quiz contains 12-15 MCQ