framework) can be arguably lumped into the systems domain as well. In addition, some
built-in functions are actually system interfaces as well—the open function, for example,
interfaces with the file system. But by and large, sys and os together form the core of
Python’s built-in system tools arsenal.
In principle at least, sys exports components related to the Python interpreter itself
(e.g., the module search path), and os contains variables and functions that map to the
operating system on which Python is run. In practice, this distinction may not always
seem clear-cut (e.g., the standard input and output streams show up in sys, but they
are arguably tied to operating system paradigms). The good news is that you’ll soon
use the tools in these modules so often that their locations will be permanently stamped
on your memory.*
The os module also attempts to provide a portable programming interface to the un-
derlying operating system; its functions may be implemented differently on different
platforms, but to Python scripts, they look the same everywhere. And if that’s still not
enough, the os module also exports a nested submodule, os.path, which provides a
portable interface to file and directory processing tools.
Module Documentation Sources
As you can probably deduce from the preceding paragraphs, learning to write system
scripts in Python is mostly a matter of learning about Python’s system modules. Luckily,
there are a variety of information sources to make this task easier—from module at-
tributes to published references and books.
For instance, if you want to know everything that a built-in module exports, you can
read its library manual entry; study its source code (Python is open source software,
after all); or fetch its attribute list and documentation string interactively. Let’s import
sys in Python 3.1 and see what it has to offer:
C:\...\PP4E\System> python
>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__',
'__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames',
'_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder',
'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle',
'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable',
'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval',
'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit',
'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion',
'int_info', 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path',
'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2',
'setcheckinterval', 'setfilesystemencoding', 'setprofile', 'setrecursionlimit',
- They may also work their way into your subconscious. Python newcomers sometimes describe a phenomenon
in which they “dream in Python” (insert overly simplistic Freudian analysis here...).
System Scripting Overview | 77