# Python Library Reference

Guido van Rossum
Corporation for National Research Initiatives (CNRI)
1895 Preston White Drive, Reston, Va 20191, USA
E-mail: `guido@CNRI.Reston.Va.US`, `guido@python.org`

December 31, 1997
Release 1.5

Copyright © 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands.

All Rights Reserved

Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI or Corporation for National Research Initiatives or CNRI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission.

While CWI is the initial source for this software, a modified version is made available by the Corporation for National Research Initiatives (CNRI) at the Internet address ftp://ftp.python.org.

STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

### Abstract:

Python is an extensible, interpreted, object-oriented programming language. It supports a wide range of applications, from simple text processing scripts to interactive WWW browsers.

While the Python Reference Manual describes the exact syntax and semantics of the language, it does not describe the standard library that is distributed with the language, and which greatly enhances its immediate usability. This library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming. Some of these modules are explicitly designed to encourage and enhance the portability of Python programs.

This library reference manual documents Python's standard library, as well as many optional library modules (which may or may not be available, depending on whether the underlying platform supports them and on the configuration choices made at compile time). It also documents the standard types of the language and its built-in functions and exceptions, many of which are not or incompletely documented in the Reference Manual.

This manual assumes basic knowledge about the Python language. For an informal introduction to Python, see the Python Tutorial; the Python Reference Manual remains the highest authority on syntactic and semantic questions. Finally, the manual entitled Extending and Embedding the Python Interpreter describes how to add new extensions to Python and how to embed it in other applications.

- Contents
- 1 Introduction
- 2 Built-in Types, Exceptions and Functions
  - 2.1 Built-in Types
    - 2.1.1 Truth Value Testing
    - 2.1.2 Boolean Operations
    - 2.1.3 Comparisons
    - 2.1.4 Numeric Types
    - 2.1.5 Sequence Types
    - 2.1.6 Mapping Types
    - 2.1.7 Other Built-in Types
    - 2.1.8 Special Attributes
  - 2.2 Built-in Exceptions
  - 2.3 Built-in Functions
- 3 Python Services
  - 3.1 Built-in Module `sys`
  - 3.2 Standard Module `types`
  - 3.3 Standard Modules `UserDict` and `UserList`
  - 3.4 Built-in Module `operator`
  - 3.5 Standard Module `traceback`
  - 3.6 Standard Module `pickle`
  - 3.7 Standard Module `shelve`
  - 3.8 Standard Module `copy`
  - 3.9 Built-in Module `marshal`
  - 3.10 Built-in Module `imp`
    - 3.10.1 Examples
  - 3.11 Built-in Module `ni`
  - 3.12 Built-in Module `parser`
    - 3.12.1 Creating AST Objects
    - 3.12.2 Converting AST Objects
    - 3.12.3 Queries on AST Objects
    - 3.12.4 Exceptions and Error Handling
    - 3.12.5 AST Objects
    - 3.12.6 Examples
  - 3.13 Standard Module `symbol`
  - 3.14 Standard Module `token`
  - 3.15 Standard Module `keyword`
  - 3.16 Standard Module `code`
  - 3.17 Standard Module `pprint`
    - 3.17.1 PrettyPrinter Objects
  - 3.18 Standard Module `dis`
    - 3.18.1 Python Byte Code Instructions
  - 3.19 Standard Module `site`
  - 3.20 Standard Module `user`
  - 3.21 Built-in Module `__builtin__`
  - 3.22 Built-in Module `__main__`
- 4 String Services
  - 4.1 Standard Module `string`
  - 4.2 Built-in Module `re`
    - 4.2.1 Regular Expression Syntax
    - 4.2.2 Module Contents
    - 4.2.3 Regular Expression Objects
    - 4.2.4 MatchObjects
  - 4.3 Built-in Module `regex`
    - 4.3.1 Regular Expressions
    - 4.3.2 Module Contents
  - 4.4 Standard Module `regsub`
  - 4.5 Built-in Module `struct`
  - 4.6 Standard Module `StringIO`
  - 4.7 Standard Module `soundex`
- 5 Miscellaneous Services
  - 5.1 Built-in Module `math`
  - 5.2 Built-in Module `cmath`
  - 5.3 Standard Module `whrandom`
  - 5.4 Standard Module `random`
  - 5.5 Standard Module `rand`
  - 5.6 Built-in Module `array`
- 6 Generic Operating System Services
  - 6.1 Standard Module `os`
  - 6.2 Built-in Module `time`
  - 6.3 Standard Module `getopt`
  - 6.4 Standard Module `tempfile`
  - 6.5 Standard Module `errno`
  - 6.6 Standard Module `glob`
  - 6.7 Standard Module `fnmatch`
  - 6.8 Standard Module `locale`
- 7 Optional Operating System Services
  - 7.1 Built-in Module `signal`
  - 7.2 Built-in Module `socket`
    - 7.2.1 Socket Objects
    - 7.2.2 Example
  - 7.3 Built-in Module `select`
  - 7.4 Built-in Module `thread`
  - 7.5 Standard Module `Queue`
    - 7.5.1 Queue Objects
  - 7.6 Standard Modules `anydbm` and `dumbdbm`
  - 7.7 Standard Module `whichdb`
  - 7.8 Built-in Module `zlib`
  - 7.9 Built-in Module `gzip`
- 8 Unix Specific Services
  - 8.1 Built-in Module `posix`
  - 8.2 Standard Module `posixpath`
  - 8.3 Built-in Module `pwd`
  - 8.4 Built-in Module `grp`
  - 8.5 Built-in Module `crypt`
  - 8.6 Built-in Module `dbm`
  - 8.7 Built-in Module `gdbm`
  - 8.8 Built-in Module `termios`
    - 8.8.1 Example
  - 8.9 Standard Module `TERMIOS`
  - 8.10 Built-in Module `fcntl`
  - 8.11 Standard Module `posixfile`
  - 8.12 Built-in Module `resource`
    - 8.12.1 Resource Limits
    - 8.12.2 Resource Usage
  - 8.13 Built-in Module `syslog`
  - 8.14 Standard Module `stat`
  - 8.15 Standard Module `commands`
- 9 The Python Debugger
  - 9.1 Debugger Commands
  - 9.2 How It Works
- 10 The Python Profiler
  - 10.1 Introduction to the profiler
  - 10.2 How Is This Profiler Different From The Old Profiler?
  - 10.3 Instant Users Manual
  - 10.4 What Is Deterministic Profiling?
  - 10.5 Reference Manual
    - 10.5.1 The `Stats` Class
  - 10.6 Limitations
  - 10.7 Calibration
  - 10.8 Extensions - Deriving Better Profilers
    - 10.8.1 OldProfile Class
    - 10.8.2 HotProfile Class
- 11 Internet and WWW Services
  - 11.1 Standard Module `cgi`
    - 11.1.1 Introduction
    - 11.1.2 Using the cgi module
    - 11.1.3 Old classes
    - 11.1.4 Functions
    - 11.1.5 Caring about security
    - 11.1.6 Installing your CGI script on a Unix system
    - 11.1.7 Testing your CGI script
    - 11.1.8 Debugging CGI scripts
    - 11.1.9 Common problems and solutions
  - 11.2 Standard Module `urllib`
  - 11.3 Standard Module `httplib`
    - 11.3.1 HTTP Objects
    - 11.3.2 Example
  - 11.4 Standard Module `ftplib`
    - 11.4.1 FTP Objects
  - 11.5 Standard Module `gopherlib`
  - 11.6 Standard Module `nntplib`
    - 11.6.1 NNTP Objects
  - 11.7 Standard Module `urlparse`
  - 11.8 Standard Module `sgmllib`
  - 11.9 Standard Module `htmllib`
  - 11.10 Standard Module `xmllib`
  - 11.11 Standard Module `formatter`
    - 11.11.1 The Formatter Interface
    - 11.11.2 Formatter Implementations
    - 11.11.3 The Writer Interface
    - 11.11.4 Writer Implementations
  - 11.12 Standard Module `rfc822`
    - 11.12.1 Message Objects
  - 11.13 Standard Module `mimetools`
    - 11.13.1 Additional Methods of Message objects
  - 11.14 Standard Module `binhex`
    - 11.14.1 notes
  - 11.15 Standard Module `uu`
  - 11.16 Built-in Module `binascii`
  - 11.17 Standard Module `xdrlib`
    - 11.17.1 Packer Objects
    - 11.17.2 Unpacker Objects
    - 11.17.3 Exceptions
  - 11.18 Standard Module `mailcap`
  - 11.19 Standard Module `base64`
  - 11.20 Standard Module `quopri`
  - 11.21 Standard Module `SocketServer`
  - 11.22 Standard Module `mailbox`
    - 11.22.1 Mailbox Objects
  - 11.23 Standard Module `mimify`
  - 11.24 Standard Module `BaseHTTPServer`
- 12 Restricted Execution
  - 12.1 Standard Module `rexec`
    - 12.1.1 An example
  - 12.2 Standard Module `Bastion`
- 13 Multimedia Services
  - 13.1 Built-in Module `audioop`
  - 13.2 Built-in Module `imageop`
  - 13.3 Standard Module `aifc`
  - 13.4 Built-in Module `jpeg`
  - 13.5 Built-in Module `rgbimg`
  - 13.6 Standard Module `imghdr`
- 14 Cryptographic Services
  - 14.1 Built-in Module `md5`
  - 14.2 Built-in Module `mpz`
  - 14.3 Built-in Module `rotor`
- 15 Macintosh Specific Services
  - 15.1 Built-in Module `mac`
  - 15.2 Standard Module `macpath`
  - 15.3 Built-in Module `ctb`
    - 15.3.1 connection object
  - 15.4 Built-in Module `macconsole`
    - 15.4.1 macconsole options object
    - 15.4.2 console window object
  - 15.5 Built-in Module `macdnr`
    - 15.5.1 dnr result object
  - 15.6 Built-in Module `macfs`
    - 15.6.1 FSSpec objects
    - 15.6.2 alias objects
    - 15.6.3 FInfo objects
  - 15.7 Standard Module `ic`
    - 15.7.1 IC objects
  - 15.8 Built-in Module `MacOS`
  - 15.9 Standard Module `macostools`
  - 15.10 Standard Module `findertools`
  - 15.11 Built-in Module `mactcp`
    - 15.11.1 TCP Stream Objects
    - 15.11.2 TCP Status Objects
    - 15.11.3 UDP Stream Objects
  - 15.12 Built-in Module `macspeech`
    - 15.12.1 voice objects
    - 15.12.2 speech channel objects
  - 15.13 Standard Module `EasyDialogs`
  - 15.14 Standard Module `FrameWork`
    - 15.14.1 Application objects
    - 15.14.2 Window Objects
    - 15.14.3 ControlsWindow Object
    - 15.14.4 ScrolledWindow Object
    - 15.14.5 DialogWindow Objects
  - 15.15 Standard Module `MiniAEFrame`
    - 15.15.1 AEServer Objects
- 16 SGI IRIX Specific Services
  - 16.1 Built-in Module `al`
    - 16.1.1 Configuration Objects
    - 16.1.2 Port Objects
  - 16.2 Standard Module `AL`
  - 16.3 Built-in Module `cd`
  - 16.4 Built-in Module `fl`
    - 16.4.1 Functions Defined in Module `fl`
    - 16.4.2 Form Objects
    - 16.4.3 FORMS Objects
  - 16.5 Standard Module `FL`
  - 16.6 Standard Module `flp`
  - 16.7 Built-in Module `fm`
  - 16.8 Built-in Module `gl`
  - 16.9 Standard Modules `GL` and `DEVICE`
  - 16.10 Built-in Module `imgfile`
- 17 SunOS Specific Services
  - 17.1 Built-in Module `sunaudiodev`
    - 17.1.1 Audio Device Objects
- 18 Undocumented Modules
  - 18.1 Fundamental, and pretty straightforward to document
  - 18.2 Frameworks; somewhat harder to document, but well worth the effort
  - 18.3 Stuff useful to a lot of people, including the CGI crowd
  - 18.4 Miscellaneous useful utilities
  - 18.5 Parsing Python
  - 18.6 Platform specific modules
  - 18.7 Code objects and files, debugger etc.
  - 18.8 Multimedia
  - 18.9 Oddities
  - 18.10 Obsolete
  - 18.11 Extension modules
- Index
- About this document ...