Hello! We've just looked at the essential building blocks of our programs. Now we're ready to start saving permanent data and reading it back!


This post is part of #100DaysOfPython, check out yesterday's post here if you haven't already. Or go to the index of all 100 days.


One of the built-in Python functions is the open() function. It lets us open files for reading or writing. We can then interact with that file.

Create a file data.txt and a file app.py for this post. Make sure they are in the same directory! We'll be using the text file to store our program data, and the Python file for our program code.

Writing to a file

with open('data.txt', 'w') as my_file:
    my_file.write('hello')

I'm sure you can sort of guess what that code does: writes 'hello' to a file called 'data.txt'.

The open() function takes two arguments:

  1. The name of the file we want to interact with; and
  2. How we want to interact with the file.

Ways of interacting with a file

We can read or write a file, but there are more ways to open a file in Python:

  • w opens the file for writing only (any content in the file will be erased!);
  • r opens the file for reading only;
  • a opens the file for appending, and any data added is placed at the end;

There are a few more modes, but they are more specialised and not necessary yet.

Reading from a file

In similar fashion, we can read the entire file contents quite easily:

content = ''
with open('data.txt', 'r') as my_file:
    content = my_file.read()

What we do in the snippet above is create a variable, which is initially an empty string. Then we open the file for reading, and place in our variable the contents of the file, as one long string.

So, if the file had the following content:

hello
my name is
Rolf

Then our lines variable would contain 'hello\nmy name is\nRolf'.

Notice how each line is separated by the \n character, which means "new line". If you're using Windows, the character may be slightly different.

Reading a file line by line

If we want to read a file line by line, we can just loop over the file. It behaves like a list. Magical!

lines = []
with open('data.txt', 'r') as my_file:
    for line in my_file:
        lines.append(line)

CSV and JSON

CSV

CSV (Comma-Separated Value) files are those where each line represents an entity, and each of the entities' values is separated by a comma. For example, the file below might represents the users of our app:

[email protected],Jose,Salvatierra
[email protected],Rolf,Smith

In this case, the fields are: email,first_name,last_name.

Reading CSV files is easy when doing it line-by-line, as we saw above:

users = []
with open('data.txt', 'r') as my_file:
    for line in my_file:
        user_fields = line.split(',')
        user = {
            'email': user_fields[0],
            'first_name': user_fields[1],
            'last_name': user_fields[2]
        }
        users.append(user)

To write a CSV file, all we have to do is separate the fields by commas when we write them. Don't forget the \n at the end of every line we write:

users = [{'email': '[email protected]', 'first_name': 'Jose', 'last_name': 'Salvatierra'}]

with open('data.txt', 'w') as my_file:
    for user in users:
        line = '{},{},{}\n'.format(user['email'], user['first_name'], user['last_name'])
        my_file.write(line)

JSON

JSON files have a slightly different format. JSON files look very much like Python dictionaries and lists:

{
  "users": [
    {
      "email": "[email protected]",
      "first_name": "Jose",
      "last_name": "Salvatierra"
    },
    {
      "email": "[email protected]",
      "first_name": "Rolf",
      "last_name": "Smith"
    }
  ]
}

Python comes with a library that we can use to read and write JSON files without having to parse them manually. For example, if we had that JSON inside our file, we could read it by doing:

import json

users = []

with open('data.txt', 'r') as my_file:
    file_contents = json.load(my_file)
    
    users = file_content['users']

It's that simple!

Similarly if we wanted to write to the file:

import json

users = [...]  # Assume users is a list of dictionaries

with open('data.txt', 'w') as my_file:
    json.dump({'users': users}, my_file)

That would create the file we saw above, given that our users variable is a list of dictionaries representing users.


We've learned about reading and writing files in Python, and we've looked at two concrete and common types of file: CSV and JSON. I'm sure you can find plenty of use cases for this!

I'll see you on the next one.