📄 node12.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//CN"><html><head><link rel="STYLESHEET" href="style.css" type='text/css' /><link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /><link rel='start' href='../index.html' title='Python Documentation Index' /><link rel="first" href="tut.html" title='Python Tutorial' /><link rel='contents' href='node2.html' title="Contents" /><link rel='last' href='about.html' title='About this document...' /><link rel='help' href='about.html' title='About this document...' /><link rel="next" href="node13.html" /><link rel="prev" href="node11.html" /><link rel="parent" href="tut.html" /><link rel="next" href="node13.html" /><meta name='aesop' content='information' /><title>10. 标准库概览 Brief Tour of the Standard Library </title></head><body><DIV CLASS="navigation"><div id='top-navigation-panel' xml:id='top-navigation-panel'><table align="center" width="100%" cellpadding="0" cellspacing="2"><tr><td class='online-navigation'><a rel="prev" title="9. 类 Classes" href="node11.html"><img src='../icons/previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td><td class='online-navigation'><a rel="parent" title="Python Tutorial" href="tut.html"><img src='../icons/up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td><td class='online-navigation'><a rel="next" title="11. What Now?" href="node13.html"><img src='../icons/next.png' border='0' height='32' alt='Next Page' width='32' /></A></td><td align="center" width="100%" class='online-navigation'>Python Tutorial</td><td class='online-navigation'><a rel="contents" title="Table of Contents" href="node2.html"><img src='../icons/contents.png' border='0' height='32' alt='Contents' width='32' /></A></td><td class='online-navigation'><img src='../icons/blank.png' border='0' height='32' alt='' width='32' /></td><td class='online-navigation'><img src='../icons/blank.png' border='0' height='32' alt='' width='32' /></td></tr></table><div class='online-navigation'><b class="navlabel">Previous:</b><a class="sectref" rel="prev" href="node11.html">9. 类 Classes</A><b class="navlabel">Up:</b><a class="sectref" rel="parent" href="tut.html">Python Tutorial</A><b class="navlabel">Next:</b><a class="sectref" rel="next" href="node13.html">11. What Now?</A></div><hr /></div></DIV><!--End of Navigation Panel--><div class='online-navigation'><!--Table of Child-Links--><A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a><UL CLASS="ChildLinks"><LI><A href="node12.html#SECTION0012100000000000000000">10.1 操作系统概览 Operating System Interface</a><LI><A href="node12.html#SECTION0012200000000000000000">10.2 文件通配符 File Wildcards</a><LI><A href="node12.html#SECTION0012300000000000000000">10.3 命令行参数 Command Line Arguments</a><LI><A href="node12.html#SECTION0012400000000000000000">10.4 错误输出重定向和程序终止 Error Output Redirection and Program Termination</a><LI><A href="node12.html#SECTION0012500000000000000000">10.5 字符串正则匹配 String Pattern Matching</a><LI><A href="node12.html#SECTION0012600000000000000000">10.6 数学 Mathematics</a><LI><A href="node12.html#SECTION0012700000000000000000">10.7 互联网访问 Internet Access</a><LI><A href="node12.html#SECTION0012800000000000000000">10.8 日期和时间 Dates and Times</a><LI><A href="node12.html#SECTION0012900000000000000000">10.9 数据压缩 Data Compression</a><LI><A href="node12.html#SECTION00121000000000000000000">10.10 性能度量 Performance Measurement</a><LI><A href="node12.html#SECTION00121100000000000000000">10.11 质量控制 Quality Control</a><LI><A href="node12.html#SECTION00121200000000000000000">10.12 Batteries Included</a></ul><!--End of Table of Child-Links--></div><HR><H1><A NAME="SECTION0012000000000000000000"></A><A NAME="briefTour"></A><BR>10. 标准库概览 Brief Tour of the Standard Library </H1><P><H1><A NAME="SECTION0012100000000000000000"></A><A NAME="os-interface"></A><BR>10.1 操作系统概览 Operating System Interface </H1><P>The <a class="ulink" href="../lib/module-os.html" ><tt class="module">os</tt></a>
module provides dozens of functions for interacting with the
operating system:<P><a class="ulink" href="../lib/module-os.html" ><tt class="module">os</tt></a> 模块提供了不少与操作系统相关联的函数。<P><div class="verbatim"><pre>
>>> import os
>>> os.system('time 0:02')
0
>>> os.getcwd() # Return the current working directory
'C:\\Python24'
>>> os.chdir('/server/accesslogs')</pre></div><P>Be sure to use the "<tt class="samp">import os</tt>" style instead of
"<tt class="samp">from os import *</tt>". This will keep <tt class="function">os.open()</tt> from
shadowing the builtin <tt class="function">open()</tt> function which operates much
differently.<P>应该用 "<tt class="samp">import os</tt>" 风格而非 "<tt class="samp">from os import
*</tt>"。这样可以保证随操作系统不同而有所变化的 <tt class="function">os.open()</tt>
不会覆盖内置函数 <tt class="function">open()</tt>。<P>The builtin <tt class="function">dir()</tt> and <tt class="function">help()</tt> functions are useful
as interactive aids for working with large modules like <tt class="module">os</tt>:<P>在使用一些像 <tt class="module">os</tt> 这样的大型模块时内置的 <tt class="function">dir()</tt> 和 <tt class="function">help()</tt> 函数非常有用。<P><div class="verbatim"><pre>
>>> import os
>>> dir(os)
<returns a list of all module functions>
>>> help(os)
<returns an extensive manual page created from the module's docstrings></pre></div><P>For daily file and directory management tasks, the
<a class="ulink" href="../lib/module-shutil.html" ><tt class="module">shutil</tt></a>
module provides a higher level interface that is easier to use:<P>针对日常的文件和目录管理任务,<a class="ulink" href="../lib/module-shutil.html" ><tt class="module">shutil</tt></a>
模块提供了一个易于使用的高级接口。<P><div class="verbatim"><pre>
>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
>>> shutil.move('/build/executables', 'installdir')</pre></div><P><H1><A NAME="SECTION0012200000000000000000"></A><A NAME="file-wildcards"></A><BR>10.2 文件通配符 File Wildcards </H1><P>The <a class="ulink" href="../lib/module-glob.html" ><tt class="module">glob</tt></a>
module provides a function for making file lists from directory
wildcard searches:<P><a class="ulink" href="../lib/module-glob.html" ><tt class="module">glob</tt></a> 模块提供了一个函数用于从目录通配符搜索中生成文件列表。<P><div class="verbatim"><pre>
>>> import glob
>>> glob.glob('*.py')
['primes.py', 'random.py', 'quote.py']</pre></div><P><H1><A NAME="SECTION0012300000000000000000"></A><A NAME="command-line-arguments"></A><BR>10.3 命令行参数 Command Line Arguments </H1><P>Common utility scripts often invoke processing command line arguments.
These arguments are stored in the
<a class="ulink" href="../lib/module-sys.html" ><tt class="module">sys</tt></a> module's <var>argv</var>
attribute as a list. For instance the following output results from
running "<tt class="samp">python demo.py one two three</tt>" at the command line:<P>通用工具脚本经常调用命令行参数。这些命令行参数以链表形式存储于 <a class="ulink" href="../lib/module-sys.html" ><tt class="module">sys</tt></a> 模块的 <var>argv</var> 变量。例如在命令行中执行 "<tt class="samp">python demo.py one two three</tt>" 后可以得到以下输出结果:<P><div class="verbatim"><pre>
>>> import sys
>>> print sys.argv
['demo.py', 'one', 'two', 'three']</pre></div><P>The <a class="ulink" href="../lib/module-getopt.html" ><tt class="module">getopt</tt></a>
module processes <var>sys.argv</var> using the conventions of the <span class="Unix">Unix</span>
<tt class="function">getopt()</tt> function. More powerful and flexible command line
processing is provided by the
<a class="ulink" href="../lib/module-optparse.html" ><tt class="module">optparse</tt></a> module.<P><a class="ulink" href="../lib/module-getopt.html" ><tt class="module">getopt</tt></a> 模块使用 <span class="Unix">Unix</span> <tt class="function">getopt()</tt> 函处理 <var>sys.argv</var>。更多的复杂命令行处理由 <a class="ulink" href="../lib/module-optparse.html" ><tt class="module">optparse</tt></a> 模块提供。<P><H1><A NAME="SECTION0012400000000000000000"></A><A NAME="stderr"></A><BR>10.4 错误输出重定向和程序终止 Error Output Redirection and Program Termination </H1><P>The <a class="ulink" href="../lib/module-sys.html" ><tt class="module">sys</tt></a>
module also has attributes for <var>stdin</var>, <var>stdout</var>, and
<var>stderr</var>. The latter is useful for emitting warnings and error
messages to make them visible even when <var>stdout</var> has been redirected:<P><a class="ulink" href="../lib/module-sys.html" ><tt class="module">sys</tt></a> 还有 <var>stdin</var>,
<var>stdout</var> 和 <var>stderr</var> 属性,即使在 <var>stdout</var>
被重定向时,后者也可以用于显示警告和错误信息。<P><div class="verbatim"><pre>
>>> sys.stderr.write('Warning, log file not found starting a new one')
Warning, log file not found starting a new one</pre></div><P>The most direct way to terminate a script is to use "<tt class="samp">sys.exit()</tt>".<P>大多脚本的定向终止都使用 "<tt class="samp">sys.exit()</tt>"。<P><H1><A NAME="SECTION0012500000000000000000"></A><A NAME="string-pattern-matching"></A><BR>10.5 字符串正则匹配 String Pattern Matching </H1><P>The <a class="ulink" href="../lib/module-re.html" ><tt class="module">re</tt></a>
module provides regular expression tools for advanced string processing.
For complex matching and manipulation, regular expressions offer succinct,
optimized solutions:<P><a class="ulink" href="../lib/module-re.html" ><tt class="module">re</tt></a> 模块为高级字符串处理提供了正则表达式工具。对于复杂的匹配和处理,正则表达式提供了简洁、优化的解决方案。<P><div class="verbatim"><pre>
>>> import re
>>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
>>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
'cat in the hat'</pre></div><P>When only simple capabilities are needed, string methods are preferred
because they are easier to read and debug:<P>如果只需要简单的功能,应该首先考虑字符串方法,因为它们非常简单,易于阅读和调试。<P><div class="verbatim"><pre>
>>> 'tea for too'.replace('too', 'two')
'tea for two'</pre></div><P><H1><A NAME="SECTION0012600000000000000000"></A><A NAME="mathematics"></A><BR>10.6 数学 Mathematics </H1><P>The <a class="ulink" href="../lib/module-math.html" ><tt class="module">math</tt></a> module gives
access to the underlying C library functions for floating point math:<P><a class="ulink" href="../lib/module-math.html" ><tt class="module">math</tt></a> 模块为浮点运算提供了对底层C函数库的访问。<P><div class="verbatim"><pre>
>>> import math
>>> math.cos(math.pi / 4.0)
0.70710678118654757
>>> math.log(1024, 2)
10.0</pre></div><P>The <a class="ulink" href="../lib/module-random.html" ><tt class="module">random</tt></a>
module provides tools for making random selections:<P><a class="ulink" href="../lib/module-random.html" ><tt class="module">random</tt></a> 提供了生成随机数的工具。<P><div class="verbatim"><pre>
>>> import random
>>> random.choice(['apple', 'pear', 'banana'])
'apple'
>>> random.sample(xrange(100), 10) # sampling without replacement
[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
>>> random.random() # random float
0.17970987693706186
>>> random.randrange(6) # random integer chosen from range(6)
4</pre></div><P><H1><A NAME="SECTION0012700000000000000000"></A><A NAME="internet-access"></A><BR>10.7 互联网访问 Internet Access </H1><P>There are a number of modules for accessing the internet and processing
internet protocols. Two of the simplest are
<a class="ulink" href="../lib/module-urllib2.html" ><tt class="module">urllib2</tt></a>
for retrieving data from urls and
<a class="ulink" href="../lib/module-smtplib.html" ><tt class="module">smtplib</tt></a>
for sending mail:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -