Introducing the os Module
As mentioned, os
is
the larger of the two core system modules. It contains all of the
usual operating-system calls you may have used in your C programs and
shell scripts. Its calls deal with directories, processes, shell
variables, and the like. Technically, this module provides POSIX
tools—a portable standard for operating-system calls—along with
platform-independent directory processing tools as the nested module
os.path
. Operationally, os
serves as a largely portable interface to
your computer’s system calls: scripts written with os
and os.path
can usually be run unchanged on any
platform.
In fact, if you read the os
module’s source code, you’ll notice that it really just imports
whatever platform-specific system module you have on your computer
(e.g., nt
, mac
, posix
). See the os.py
file in the Python source library directory—it simply runs a from*
statement to copy all names out of a
platform-specific module. By always importing os
rather than platform-specific modules,
though, your scripts are mostly immune to platform implementation
differences. On some platforms, os
includes extra tools available just for that platform (e.g., low-level
process calls on Unix); by and large, though, it is as cross-platform
as it is technically feasible.
The Big os Lists
Let’s take a quick look at the basic interfaces in os
. As a
preview, Table 3-1
summarizes some of the most commonly used tools in the os
module organized by functional
area.
Table 3-1. Commonly used os ...
Get Programming Python, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.