Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The notion of the current working directory (CWD) turns out to be a key
concept in some scripts’ execution: it’s always the implicit place
where files processed by the script are assumed to reside unless their
names have absolute directory paths. As we saw earlier, os.getcwd lets a script fetch the CWD name
explicitly, and os.chdir allows a
script to move to a new CWD.
Keep in mind, though, that filenames without full pathnames map
to the CWD and have nothing to do with your PYTHONPATH setting. Technically, a script is always launched from the CWD,
not the directory containing the script file. Conversely, imports
always first search the directory containing the script, not the CWD
(unless the script happens to also be located in the CWD). Since this
distinction is subtle and tends to trip up beginners, let’s explore it
in a bit more detail.