Working with Files

Python provides a built-in function for opening files, cleverly titled open(). The open() method will return an object that you can read() to get the data. By default, open() will open a file in read-only mode, however you can change this by passing a mode parameter. The list of optional modes is here:

Character Meaning
‘r’ open for reading (default)
‘w’ open for writing, truncating the file first
‘x’ open for exclusive creation, failing if the file already exists
‘a’ open for writing, appending to the end of the file if it exists
‘b’ binary mode
’t’ text mode (default)
’+’ open a disk file for updating (reading and writing)

Opening a file looks like this:

# Open a file for reading
>>> my_file = open("my_file.txt")

# Open a file for reading or writing
# This will replace any existing file
>>> my_file = open("my_file.txt", "w")

# Open a file for reading or writing
# This will append to the end of any existing file
>>> my_file = open("my_file.txt", "a")

Of course, you always want to call close() on your open file object once you’re done with it, so that your program doesn’t leave open file handles dangling. But what happens if your program exits or crashes before you can close a file? You can use a Context Manager.

Context Managers

A Context Manager is like a wrapper around a block of code that depends on some resource. It’s a safer way of handling resources than, say, using open() and then remembering to close() later (and hoping your program doesn’t crash in between). It’s similar to using try... finally, but cleaner to look at. Context managers can contain code that auto-magically provisions a resource before your code runs, and cleans up afterward. For example, the open() function also works as a context manager, so opening a file looks like this:

>>> with open("my_file.text") as my_file:
...     contents = my_file.read()

You can perform all the functions that require access to the open file within the with scope, and once you exit the with scope, the context manager will automatically close your file for you, even if your code hits an exception inside the context manager.

Working with Files

Let’s try working with a simple json file. Download this cities.json file, which contains a listing of the top five cities in the US by population. We’ll import the json module to help us parse the data, and we’ll use a context manager to open the file.

>>> import json
>>> with open("cities.json") as cities_file:
...     cities_data = json.load(cities_file)
...     print(cities_data)
...
>>> # Let's pretty-print the results
>>> from pprint import pprint
>>> pprint(cities_data)
[{'name': 'New York', 'pop': 8550405},
 {'name': 'Los Angeles', 'pop': 3971883},
 {'name': 'Chicago', 'pop': 2720546},
 {'name': 'Houston', 'pop': 2296224},
 {'name': 'Philadelphia', 'pop': 1567442}]
>>>