How to Write a List to a File in Python
TLDR: Print items line by line
Use this method to write items of a Python list one by one to a text file.
Python 3
cities = ['Atlanta', 'Boston', 'Chicago', 'Los Angeles', 'New York', 'San Francisco', 'Seattle']
FILE_PATH = './cities.txt'
with open(FILE_PATH, 'w') as output_file:
for city in cities:
print(city, file=output_file)
Python 2
cities = ['Atlanta', 'Boston', 'Chicago', 'Los Angeles', 'New York', 'San Francisco', 'Seattle']
FILE_PATH = './cities.txt'
with open(FILE_PATH, 'w') as output_file:
for city in cities:
print >> output_file, city
The output in ./cities.txt
will look like this:
Atlanta
Boston
Chicago
Los Angeles
New York
San Francisco
Seattle
Write a Python list to a JSON file
Say you have a list of complex objects, or you need a simple way to read the list back into another program. JSON is the way to go, and doing this in Python is as simple as calling a function.
import json
cities = [
{
'city': 'Atlanta',
'state': 'Georgia'
},
{
'city': 'Boston',
'state': 'Massachusetts'
},
{
'city': 'Los Angeles',
'state': 'California'
},
{
'city': 'Seattle',
'state': 'Washington'
}
]
FILE_PATH = './cities.json'
with open(FILE_PATH, 'w') as output_file:
json.dump(cities, output_file, indent=2)
As you might guess, here is our output in ./cities.json
:
[
{
"city": "Atlanta",
"state": "Georgia"
},
{
"city": "Boston",
"state": "Massachusetts"
},
{
"city": "Los Angeles",
"state": "California"
},
{
"city": "Seattle",
"state": "Washington"
}
]
How it works
First off, we need to import the json
module from Python's standard library.
import json
Once we have our list, we open a file for writing.
FILE_PATH = './cities.json'
with open(FILE_PATH, 'w') as output_file:
...
Using with
statement to open files is a good practice because it ensures that the resource (file) opened with open(FILE_PATH, 'w')
is closed after the execution flow leaves the with
block.
We then call json.dump(cities, output_file, indent=2)
to actually write the list to the file.
The json.dump
function has a number of useful parameters - here we've used indent=2
. It pretty-prints our json using 2 spaces as unit of indentation. Without it, the output would look like this:
[{"city": "Atlanta", "state": "Georgia"}, {"city": "Boston", "state": "Massachusetts"}, {"city": "Los Angeles", "state": "California"}, {"city": "Seattle", "state": "Washington"}]
More compact, but hardly human-readable.
Another useful parameter to json.dump
is sort_keys=True
. It ensures that all keys of every dictionary of our object are output in sorted order.
Write a Python list to a JSON string
We've seen how json.dump(list, file)
writes a list to a file. It's often useful to dump an object or a list to a string, eg. for sending as a request payload to an API.
We can achieve this by simply calling the json.dumps(object)
method, which returns a JSON representation of the object as a plain Python string.
Combining this with our first method of writing lists to files using print
, we can also write lists to JSON files in the following way:
import json
cities = ['Atlanta', 'Boston', 'Chicago', 'Los Angeles', 'New York', 'San Francisco', 'Seattle']
FILE_PATH = './cities.json'
with open(FILE_PATH, 'w') as output_file:
print(json.dumps(cities, indent=2), file=output_file)