System Scripting Overview

We will take a quick tour through the standard library sys and os modules in the first few sections of this chapter before moving on to larger system programming concepts. As you can tell from the length of their attribute lists, both of these are large modules (their content may vary slightly per Python version and platform):

>>>import sys, os
>>> len(dir(sys))          # 56 attributes
>>> len(dir(os))           # 118 on Windows, more on Unix
>>> len(dir(os.path))      # a nested module within os

As I’m not going to demonstrate every item in every built-in module, the first thing I want to do is show you how to get more details on your own. Officially, this task also serves as an excuse for introducing a few core system scripting concepts; along the way, we’ll code a first script to format documentation.

Python System Modules

Most system-level interfaces in Python are shipped in just two modules: sys and os. That’s somewhat oversimplified; other standard modules belong to this domain too. Among them are the following:


For filename expansion


For network connections and Inter-Process Communication (IPC)

thread and queue

For concurrent threads


For accessing system time details


For low-level file control

In addition, some built-in functions are actually system interfaces as well (e.g., open). But sys and os together form the core of Python’s system tools arsenal.

In principle at least, sys exports components related to the Python interpreter itself (e.g., the module ...

Get Programming Python, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.