DEV Community

Cover image for How to Parse /etc/os-release
Hai Vu
Hai Vu

Posted on

How to Parse /etc/os-release

The Problem

As part of my daily tasks, I need to parse the /etc/os-release file for information using Python. I have seen various attempts from other people, most of which are too complicated, too cumbersome, so I came up with my own.

Below is a sample contents.

VERSION="20.04.3 LTS (Focal Fossa)"
PRETTY_NAME="Ubuntu 20.04.3 LTS"
Enter fullscreen mode Exit fullscreen mode

The Solution

The easiest way to parse this file is to you the PyYAML library. However, in some projects, my team does not have access to third-party libraries and have to stay within the Python 3.7's standard libraries.

After several attempts, I came up with a fairly easy solution: Use the csv library. Below is a code snippet which parses /etc/os-release and returns a dictionary.

import csv
import pathlib
from pprint import pprint

path = pathlib.Path("/etc/os-release")
with open(path) as stream:
    reader = csv.reader(stream, delimiter="=")
    os_release = dict(reader)

Enter fullscreen mode Exit fullscreen mode

Here is the output:

 'HOME_URL': '',
 'ID': 'ubuntu',
 'ID_LIKE': 'debian',
 'NAME': 'Ubuntu',
 'PRETTY_NAME': 'Ubuntu 20.04.3 LTS',
 'UBUNTU_CODENAME': 'focal',
 'VERSION': '20.04.3 LTS (Focal Fossa)',
 'VERSION_ID': '20.04'}
Enter fullscreen mode Exit fullscreen mode

How it Works

  • The file contains lines of key=value, so I use the csv library to read it and specify the equal sign as fields delimiters.
  • The reader will yield a list of (key, value) tuples
  • The dict built-in turns that list of tuples into a dictionary


After several attempts, I like this solution the best because it leaves the parsing to a standard library, which I assume well tested.

Discussion (0)