Python scripts don’t run in a vacuum (despite what you may have heard). Depending on platforms and startup procedures, Python programs may have all sorts of enclosing context—information automatically passed in to the program by the operating system when the program starts up. For instance, scripts have access to the following sorts of system-level inputs and interfaces:
access to the directory from which a script is
started, and many file tools use its value implicitly.
access to words typed on the command line that are
used to start the program and that serve as script
an interface to names assigned in the enclosing
shell (or a parent program) and passed in to the script.
stderr export the three input/output streams that are at the heart of
command-line shell tools, and can be leveraged by scripts with
introduced in Chapter 2, the
io.StringIO class, and more.
Such tools can serve as inputs to scripts, configuration parameters, and so on. In this chapter, we will explore all these four context’s tools—both their Python interfaces and their typical roles.
The notion of the current working directory (CWD) turns out to be a key concept in some scripts’ execution: it’s always the ...