Python 标准库¶
Python 语言参考 描述了 Python 语言的具体语法和语义,这份库参考则介绍了与 Python 一同发行的标准库。它还描述了通常包含在 Python 发行版中的一些可选组件。
Python 标准库非常庞大,所提供的组件涉及范围十分广泛,正如以下内容目录所显示的。这个库包含了多个内置模块 (以 C 编写),Python 程序员必须依靠它们来实现系统级功能,例如文件 I/O,此外还有大量以 Python 编写的模块,提供了日常编程中许多问题的标准解决方案。其中有些模块经过专门设计,通过将特定平台功能抽象化为平台中立的 API 来鼓励和加强 Python 程序的可移植性。
Windows 版本的 Python 安装程序通常包含整个标准库,往往还包含许多额外组件。对于类 Unix 操作系统,Python 通常会分成一系列的软件包,因此可能需要使用操作系统所提供的包管理工具来获取部分或全部可选组件。
在这个标准库以外还存在成千上万并且不断增加的其他组件 (从单独的程序、模块、软件包直到完整的应用开发框架),访问 Python 包索引 即可获取这些第三方包。
- 1. 概述
- 2. 内置函数
- 3. Non-essential Built-in Functions
- 4. 内置常量
- 5. 内置类型
- 5.1. 逻辑值检测
- 5.2. Boolean Operations —
and
,or
,not
- 5.3. 比较
- 5.4. Numeric Types —
int
,float
,long
,complex
- 5.5. 迭代器类型
- 5.6. Sequence Types —
str
,unicode
,list
,tuple
,bytearray
,buffer
,xrange
- 5.7. 集合类型 —
set
,frozenset
- 5.8. 映射类型 —
dict
- 5.9. File Objects
- 5.10. memoryview type
- 5.11. Context Manager Types
- 5.12. Other Built-in Types
- 5.13. 特殊属性
- 6. 内置异常
- 7. String Services
- 7.1.
string
— 常见的字符串操作 - 7.2.
re
— 正则表达式操作 - 7.3.
struct
— Interpret strings as packed binary data - 7.4. 模块 difflib 是一个计算差异的助手
- 7.5.
StringIO
— Read and write strings as files - 7.6.
cStringIO
— Faster version ofStringIO
- 7.7.
textwrap
— Text wrapping and filling - 7.8.
codecs
— Codec registry and base classes - 7.9.
unicodedata
— Unicode 数据库 - 7.10.
stringprep
— Internet String Preparation - 7.11.
fpformat
— Floating point conversions
- 7.1.
- 8. 数据类型
- 8.1.
datetime
— 基础日期/时间数据类型 - 8.2.
calendar
— General calendar-related functions - 8.3.
collections
— High-performance container datatypes - 8.4.
heapq
— Heap queue algorithm - 8.5.
bisect
— Array bisection algorithm - 8.6.
array
— Efficient arrays of numeric values - 8.7.
sets
— Unordered collections of unique elements - 8.8.
sched
— 事件调度器 - 8.9.
mutex
— Mutual exclusion support - 8.10.
Queue
— A synchronized queue class - 8.11.
weakref
— 弱引用 - 8.12.
UserDict
— Class wrapper for dictionary objects - 8.13.
UserList
— Class wrapper for list objects - 8.14.
UserString
— Class wrapper for string objects - 8.15.
types
— Names for built-in types - 8.16.
new
— Creation of runtime internal objects - 8.17.
copy
— 浅层 (shallow) 和深层 (deep) 复制操作 - 8.18.
pprint
— 数据美化输出 - 8.19.
repr
— Alternaterepr()
implementation
- 8.1.
- 9. 数字和数学模块
- 10. 文件和目录访问
- 10.1.
os.path
— 常见路径操作 - 10.2.
fileinput
— Iterate over lines from multiple input streams - 10.3.
stat
— Interpretingstat()
results - 10.4.
statvfs
— Constants used withos.statvfs()
- 10.5.
filecmp
— File and Directory Comparisons - 10.6.
tempfile
— Generate temporary files and directories - 10.7.
glob
— Unix style pathname pattern expansion - 10.8.
fnmatch
— Unix filename pattern matching - 10.9.
linecache
— Random access to text lines - 10.10.
shutil
— High-level file operations - 10.11.
dircache
— Cached directory listings - 10.12.
macpath
— Mac OS 9 路径操作函数
- 10.1.
- 11. 数据持久化
- 11.1.
pickle
—— Python 对象序列化 - 11.2.
cPickle
— A fasterpickle
- 11.3.
copy_reg
— Registerpickle
support functions - 11.4.
shelve
— Python object persistence - 11.5.
marshal
— Internal Python object serialization - 11.6.
anydbm
— Generic access to DBM-style databases - 11.7.
whichdb
— Guess which DBM module created a database - 11.8.
dbm
— Simple “database” interface - 11.9.
gdbm
— GNU’s reinterpretation of dbm - 11.10.
dbhash
— DBM-style interface to the BSD database library - 11.11.
bsddb
— Interface to Berkeley DB library - 11.12.
dumbdbm
— Portable DBM implementation - 11.13.
sqlite3
— SQLite 数据库 DB-API 2.0 接口模块
- 11.1.
- 12. 数据压缩和存档
- 13. 文件格式
- 14. 加密服务
- 15. 通用操作系统服务
- 15.1.
os
— 操作系统接口模块 - 15.2.
io
— 处理流的核心工具 - 15.3.
time
— 时间的访问和转换 - 15.4.
argparse
— 命令行选项、参数和子命令解析器 - 15.5.
optparse
— Parser for command line options - 15.6.
getopt
— C-style parser for command line options - 15.7.
logging
— Logging facility for Python - 15.8.
logging.config
— 日志记录配置 - 15.9.
logging.handlers
— Logging handlers - 15.10.
getpass
— 便携式密码输入工具 - 15.11.
curses
— 终端字符单元显示的处理 - 15.12.
curses.textpad
— Text input widget for curses programs - 15.13.
curses.ascii
— Utilities for ASCII characters - 15.14.
curses.panel
— A panel stack extension for curses - 15.15.
platform
— Access to underlying platform’s identifying data - 15.16.
errno
— Standard errno system symbols - 15.17.
ctypes
— Python 的外部函数库
- 15.1.
- 16. Optional Operating System Services
- 16.1.
select
— Waiting for I/O completion - 16.2.
threading
— Higher-level threading interface - 16.3.
thread
— Multiple threads of control - 16.4.
dummy_threading
— 可直接替代threading
模块。 - 16.5.
dummy_thread
— Drop-in replacement for thethread
module - 16.6.
multiprocessing
— Process-based “threading” interface - 16.7.
mmap
— Memory-mapped file support - 16.8.
readline
— GNU readline interface - 16.9.
rlcompleter
— GNU readline的完成函数
- 16.1.
- 17. Interprocess Communication and Networking
- 18. 互联网数据处理
- 18.1.
email
— 电子邮件与 MIME 处理包 - 18.2.
json
— JSON 编码和解码器 - 18.3.
mailcap
— Mailcap file handling - 18.4.
mailbox
— Manipulate mailboxes in various formats - 18.5.
mhlib
— Access to MH mailboxes - 18.6.
mimetools
— Tools for parsing MIME messages - 18.7.
mimetypes
— Map filenames to MIME types - 18.8.
MimeWriter
— Generic MIME file writer - 18.9.
mimify
— MIME processing of mail messages - 18.10.
multifile
— Support for files containing distinct parts - 18.11.
rfc822
— Parse RFC 2822 mail headers - 18.12.
base64
— RFC 3548: Base16, Base32, Base64 Data Encodings - 18.13.
binhex
— 对binhex4文件进行编码和解码 - 18.14.
binascii
— 二进制和 ASCII 码互转 - 18.15.
quopri
— Encode and decode MIME quoted-printable data - 18.16.
uu
— Encode and decode uuencode files
- 18.1.
- 19. 结构化标记处理工具
- 19.1.
HTMLParser
— Simple HTML and XHTML parser - 19.2.
sgmllib
— Simple SGML parser - 19.3.
htmllib
— A parser for HTML documents - 19.4.
htmlentitydefs
— Definitions of HTML general entities - 19.5. XML处理模块
- 19.6. XML 漏洞
- 19.7.
xml.etree.ElementTree
— The ElementTree XML API - 19.8.
xml.dom
— The Document Object Model API - 19.9.
xml.dom.minidom
— Minimal DOM implementation - 19.10.
xml.dom.pulldom
— Support for building partial DOM trees - 19.11.
xml.sax
— Support for SAX2 parsers - 19.12.
xml.sax.handler
— Base classes for SAX handlers - 19.13.
xml.sax.saxutils
— SAX Utilities - 19.14.
xml.sax.xmlreader
— Interface for XML parsers - 19.15.
xml.parsers.expat
— Fast XML parsing using Expat
- 19.1.
- 20. 互联网协议和支持
- 20.1.
webbrowser
— 方便的Web浏览器控制器 - 20.2.
cgi
— Common Gateway Interface support - 20.3.
cgitb
— Traceback manager for CGI scripts - 20.4.
wsgiref
— WSGI Utilities and Reference Implementation - 20.5.
urllib
— Open arbitrary resources by URL - 20.6.
urllib2
— extensible library for opening URLs - 20.7.
httplib
— HTTP protocol client - 20.8.
ftplib
— FTP protocol client - 20.9.
poplib
— POP3 protocol client - 20.10.
imaplib
— IMAP4 protocol client - 20.11.
nntplib
— NNTP protocol client - 20.12.
smtplib
—SMTP协议客户端 - 20.13.
smtpd
— SMTP Server - 20.14.
telnetlib
— Telnet client - 20.15.
uuid
— UUID objects according to RFC 4122 - 20.16.
urlparse
— Parse URLs into components - 20.17.
SocketServer
— A framework for network servers - 20.18.
BaseHTTPServer
— Basic HTTP server - 20.19.
SimpleHTTPServer
— Simple HTTP request handler - 20.20.
CGIHTTPServer
— CGI-capable HTTP request handler - 20.21.
cookielib
— Cookie handling for HTTP clients - 20.22.
Cookie
— HTTP state management - 20.23.
xmlrpclib
— XML-RPC client access - 20.24.
SimpleXMLRPCServer
— Basic XML-RPC server - 20.25.
DocXMLRPCServer
— Self-documenting XML-RPC server
- 20.1.
- 21. 多媒体服务
- 21.1.
audioop
— Manipulate raw audio data - 21.2.
imageop
— Manipulate raw image data - 21.3.
aifc
— Read and write AIFF and AIFC files - 21.4.
sunau
— 读写 Sun AU 文件 - 21.5.
wave
— 读写WAV格式文件 - 21.6.
chunk
— Read IFF chunked data - 21.7.
colorsys
— Conversions between color systems - 21.8.
imghdr
— 推测图像类型 - 21.9.
sndhdr
— 推测声音文件的类型 - 21.10.
ossaudiodev
— Access to OSS-compatible audio devices
- 21.1.
- 22. 国际化
- 23. 程序框架
- 24. Tk图形用户界面(GUI)
- 25. 开发工具
- 26. 调试和分析
- 27. 软件打包和分发
- 28. Python运行时服务
- 28.1.
sys
— 系统相关的参数和函数 - 28.2.
sysconfig
— Provide access to Python’s configuration information - 28.3.
__builtin__
— Built-in objects - 28.4.
future_builtins
— Python 3 builtins - 28.5.
__main__
— 顶层脚本环境 - 28.6.
warnings
— Warning control - 28.7.
contextlib
— Utilities forwith
-statement contexts - 28.8.
abc
— 抽象基类 - 28.9.
atexit
— 退出处理器 - 28.10.
traceback
— Print or retrieve a stack traceback - 28.11.
__future__
— Future 语句定义 - 28.12.
gc
— 垃圾回收器接口 - 28.13.
inspect
— 检查对象 - 28.14.
site
— Site-specific configuration hook - 28.15.
user
— User-specific configuration hook - 28.16.
fpectl
— Floating point exception control
- 28.1.
- 29. 自定义 Python 解释器
- 30. Restricted Execution
- 31. 导入模块
- 31.1.
imp
— Access theimport
internals - 31.2.
importlib
— Convenience wrappers for__import__()
- 31.3.
imputil
— Import utilities - 31.4.
zipimport
— Import modules from Zip archives - 31.5.
pkgutil
— Package extension utility - 31.6.
modulefinder
— 查找脚本使用的模块 - 31.7.
runpy
— Locating and executing Python modules
- 31.1.
- 32. Python 语言服务
- 32.1.
parser
— Access Python parse trees - 32.2.
ast
— 抽象语法树 - 32.3.
symtable
— Access to the compiler’s symbol tables - 32.4.
symbol
— 与 Python 解析树一起使用的常量 - 32.5.
token
— 与Python解析树一起使用的常量 - 32.6.
keyword
— 检验Python关键字 - 32.7.
tokenize
— Tokenizer for Python source - 32.8.
tabnanny
— 模糊缩进检测 - 32.9.
pyclbr
— Python class browser support - 32.10.
py_compile
— Compile Python source files - 32.11.
compileall
— Byte-compile Python libraries - 32.12.
dis
— Python 字节码反汇编器 - 32.13.
pickletools
— Tools for pickle developers
- 32.1.
- 33. Python compiler package
- 34. 杂项服务
- 35. Windows系统相关模块
- 36. Unix 专有服务
- 36.1.
posix
— The most common POSIX system calls - 36.2.
pwd
— 用户密码数据库 - 36.3.
spwd
— The shadow password database - 36.4.
grp
— The group database - 36.5.
crypt
— Function to check Unix passwords - 36.6.
dl
— Call C functions in shared objects - 36.7.
termios
— POSIX style tty control - 36.8.
tty
— 终端控制功能 - 36.9.
pty
— Pseudo-terminal utilities - 36.10.
fcntl
— Thefcntl
andioctl
system calls - 36.11.
pipes
— Interface to shell pipelines - 36.12.
posixfile
— File-like objects with locking support - 36.13.
resource
— Resource usage information - 36.14.
nis
— Interface to Sun’s NIS (Yellow Pages) - 36.15. Unix syslog 库例程
- 36.16.
commands
— Utilities for running commands
- 36.1.
- 37. Mac OS X specific services
- 37.1.
ic
— Access to the Mac OS X Internet Config - 37.2.
MacOS
— Access to Mac OS interpreter features - 37.3.
macostools
— Convenience routines for file manipulation - 37.4.
findertools
— The finder’s Apple Events interface - 37.5.
EasyDialogs
— Basic Macintosh dialogs - 37.6.
FrameWork
— Interactive application framework - 37.7.
autoGIL
— Global Interpreter Lock handling in event loops - 37.8. Mac OS Toolbox Modules
- 37.9.
ColorPicker
— Color selection dialog
- 37.1.
- 38. MacPython OSA Modules
- 39. SGI IRIX Specific Services
- 39.1.
al
— Audio functions on the SGI - 39.2.
AL
— Constants used with theal
module - 39.3.
cd
— CD-ROM access on SGI systems - 39.4.
fl
— FORMS library for graphical user interfaces - 39.5.
FL
— Constants used with thefl
module - 39.6.
flp
— Functions for loading stored FORMS designs - 39.7.
fm
— Font Manager interface - 39.8.
gl
— Graphics Library interface - 39.9.
DEVICE
— Constants used with thegl
module - 39.10.
GL
— Constants used with thegl
module - 39.11.
imgfile
— Support for SGI imglib files - 39.12.
jpeg
— Read and write JPEG files
- 39.1.
- 40. SunOS Specific Services
- 41. 未创建文档的模块