[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
>>> len(dir(os)) # 122 on Windows, more on Unix
122
>>> len(dir(os.path)) # a nested module within os
52

The content of these two modules may vary per Python version and platform. For
example, os is much larger under Cygwin after building Python 3.1 from its source code
there (Cygwin is a system that provides Unix-like functionality on Windows; it is dis-
cussed further in “More on Cygwin Python for Windows” on page 185):


$ ./python.exe
Python 3.1.1 (r311:74480, Feb 20 2010, 10:16:52)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, os
>>> len(dir(sys))
64
>>> len(dir(os))
217
>>> len(dir(os.path))
51

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:


glob
For filename expansion


socket
For network connections and Inter-Process Communication (IPC)


threading, _thread, queue
For running and synchronizing concurrent threads


time, timeit
For accessing system time details


subprocess, multiprocessing
For launching and controlling parallel processes


signal, select, shutil, tempfile, and others
For various other system-related tasks


Third-party extensions such as pySerial (a serial port interface), Pexpect (an Expect
work-alike for controlling cross-program dialogs), and even Twisted (a networking


76 | Chapter 2: System Tools

Free download pdf