📄 node4.html
字号:
<P><H2><A NAME="SECTION004210000000000000000"></A><A NAME="error"></A><BR>2.2.1 错误处理 Error Handling </H2><P>When an error occurs, the interpreter prints an error
message and a stack trace. In interactive mode, it then returns to
the primary prompt; when input came from a file, it exits with a
nonzero exit status after printing
the stack trace. (Exceptions handled by an <tt class="keyword">except</tt> clause in a
<tt class="keyword">try</tt> statement are not errors in this context.) Some errors are
unconditionally fatal and cause an exit with a nonzero exit; this
applies to internal inconsistencies and some cases of running out of
memory. All error messages are written to the standard error stream;
normal output from the executed commands is written to standard
output.<P>有错误发生时,解释器打印一个错误信息和栈跟踪器。交互模式下,它返回主提示符,如果从文件输入执行,它在打印栈跟踪器后以非零状态退出。(异常可以由
<tt class="keyword">try</tt> 语句中的 <tt class="keyword">except</tt>
子句来控制,这样就不会出现上文中的错误信息)有一些非常致命的错误会导致非零状态下退出,这由通常由内部矛盾和内存溢出造成。所有的错误信息都写入标准错误流;命令中执行的普通输出写入标准输出。<P>Typing the interrupt character (usually Control-C or DEL) to the
primary or secondary prompt cancels the input and returns to the
primary prompt.<A NAME="tex2html1" HREF="#foot132"><SUP>2.1</SUP></A> Typing an interrupt while a command is executing raises the
<tt class="exception">KeyboardInterrupt</tt> exception, which may be handled by a
<tt class="keyword">try</tt> statement.<P>在主提示符或附属提示符输入中断符(通常是Control-C or
DEL)就会取消当前输入,回到主命令行。 <A NAME="tex2html2" HREF="#foot135"><SUP>2.2</SUP></A>.执行命令时输入一个中断符会抛出一个
<tt class="exception">KeyboardInterrupt</tt> 异常,它可以被 <tt class="keyword">try</tt>
句截获。<P><H2><A NAME="SECTION004220000000000000000"></A><A NAME="scripts"></A><BR>2.2.2 执行Python脚本 Executable Python Scripts </H2><P>On BSD'ish <span class="Unix">Unix</span> systems, Python scripts can be made directly
executable, like shell scripts, by putting the line<P>BSD类的 <span class="Unix">Unix</span>系统中,Python 脚本可以像 Shell
脚本那样直接执行。只要在脚本文件开头写一行命令,指定文件和模式:<P><div class="verbatim"><pre>
#! /usr/bin/env python</pre></div><P>(assuming that the interpreter is on the user's <a class="envvar" id='l2h-1' xml:id='l2h-1'>PATH</a>) at
the beginning of the script and giving the file an executable
mode. The "<tt class="samp">#!</tt>" must be the first two characters of the
file. On some platforms, this first line must end with a
<span class="Unix">Unix</span>-style line ending ("<tt class="character">\n</tt>"), not a Mac OS
("<tt class="character">\r</tt>") or Windows ("<tt class="character">\r\n</tt>") line ending.
Note that the hash, or pound, character, "<tt class="character">#</tt>", is used
to start a comment in Python.<P>(将用户路径通知解释器) "<tt class="samp">#!</tt>"
必须是文件的前两个字符,在某些平台上,第一行必须以 <span class="Unix">Unix</span>
风格的行结束符("<tt class="character">\n</tt>")结束,不能用Mac("<tt class="character">\
r</tt>")或Windows("<tt class="character">\r\
n</tt>")的结束符。注意,"<tt class="character">#</tt>"是Python中是行注释的起始符。<P>The script can be given a executable mode, or permission, using
the <b class="program">chmod</b> command:<P>脚本可以通过 <b class="program">chmod</b> 命令指定执行模式和许可权。<P><div class="verbatim"><pre>
$ chmod +x myscript.py</pre></div> <P><H2><A NAME="SECTION004230000000000000000">2.2.3 源程序编码 Source Code Encoding</A></H2><P>It is possible to use encodings different than ASCII in Python source
files. The best way to do it is to put one more special comment line
right after the <code>#!</code> line to define the source file encoding:<P>Python 的源文件可以通过编码使用 ASCII 以外的字符集。
最好的做法是在 <code>#!</code> 行后面用一个特殊的注释行来定义字符集。<P><div class="verbatim"><pre>
# -*- coding: iso-8859-1 -*-</pre></div><P>With that declaration, all characters in the source file will be treated as
<code>iso-8859-1</code>, and it will be
possible to directly write Unicode string literals in the selected
encoding. The list of possible encodings can be found in the
<em class="citetitle"><a href="../lib/lib.html" title="Python Library Reference" >Python Library Reference</a></em>, in the section
on <a class="ulink" href="../lib/module-codecs.html" ><tt class="module">codecs</tt></a>.<P>根据这个声明,Python 会将文件中的字符尽可能的从指定的编码转为
Unicode,在本例中,这个字符集是 iso-8859-1 。在
<em class="citetitle"><a href="../lib/lib.html" title="Python 库参考手册" >Python 库参考手册</a></em> 中
<a class="ulink" href="../lib/module-codecs.html" ><tt class="module">codecs</tt></a>部份可以找到可用的编码列表(根据个人经验,推荐使用
cp-936或utf-8处理中文--译者注)。<P>If your editor supports saving files as <code>UTF-8</code> with a UTF-8
<em>byte order mark</em> (aka BOM), you can use that instead of an
encoding declaration. IDLE supports this capability if
<code>Options/General/Default Source Encoding/UTF-8</code> is set. Notice
that this signature is not understood in older Python releases (2.2
and earlier), and also not understood by the operating system for
<code>#!</code> files.<P>如果你的文件编辑器支持 <code>UTF-8</code> 格式,并且可以保存
<code>UTF-8</code> 标记(<em>aka BOM - Byte Order
Mark</em>),你可以用这个来代替编码声明。IDLE可以通过设定<code>Options/General/Default
Source Encoding/UTF-8</code>
来支持它。需要注意的是旧版Python不支持这个标记(Python
2.2或更早的版本),同样支持<code>#!</code>文件的操作系统也不会支持它(即<code>#!</code>和
# -*- coding: -*- 二者必择其一——译者)。<P>By using UTF-8 (either through the signature or an encoding
declaration), characters of most languages in the world can be
used simultaneously in string literals and comments. Using
non-ASCIIcharacters in identifiers is not supported. To display
all these characters properly, your editor must recognize that the
file is UTF-8, and it must use a font that supports all the
characters in the file.<P>使用 UTF-8
内码(无论是用标记还是编码声明),我们可以在字符串和注释中使用世界上的大部分语言。标识符中不能使用非
ASCII
字符集。为了正确显示所有的字符,你一定要在编辑器中将文件保存为
UTF-8 格式,而且要使用支持文件中所有字符的字体。<P><H2><A NAME="SECTION004240000000000000000"></A><A NAME="startup"></A><BR>2.2.4 交互式环境的启动文件 The Interactive Startup File </H2><P>When you use Python interactively, it is frequently handy to have some
standard commands executed every time the interpreter is started. You
can do this by setting an environment variable named
<a class="envvar" id='l2h-2' xml:id='l2h-2'>PYTHONSTARTUP</a> to the name of a file containing your start-up
commands. This is similar to the <span class="file">.profile</span> feature of the
<span class="Unix">Unix</span> shells.<P>使用 Python
解释器的时候,我们可能需要在每次解释器启动时执行一些命令。你可以在一个文件中包含你想要执行的命令,设定一个名为
<a class="envvar" id='l2h-3' xml:id='l2h-3'>PYTHONSTARTUP</a> 的环境变量来指定这个文件。这类似于 Unix
shell的 <span class="file">.profile</span> 文件。<P>This file is only read in interactive sessions, not when Python reads
commands from a script, and not when <span class="file">/dev/tty</span> is given as the
explicit source of commands (which otherwise behaves like an
interactive session). It is executed in the same namespace where
interactive commands are executed, so that objects that it defines or
imports can be used without qualification in the interactive session.
You can also change the prompts <code>sys.ps1</code> and <code>sys.ps2</code> in
this file.<P>这个文件在交互会话期是只读的,当 Python 从脚本中解读文件或以终端
<span class="file">/dev/tty</span>
做为外部命令源时则不会如此(尽管它们的行为很像是处在交互会话期。)它与解释器执行的命令处在同一个命名空间,所以由它定义或引用的一切可以在解释器中不受限制的使用。你也可以在这个文件中改变
<code>sys.ps1</code> 和 <code>sys.ps2</code> 指令。<P>If you want to read an additional start-up file from the current
directory, you can program this in the global start-up file using code
like "<tt class="samp">if os.path.isfile('.pythonrc.py'):
execfile('.pythonrc.py')</tt>". If you want to use the startup file in a
script, you must do this explicitly in the script:<P>如果你想要在当前目录中执行附加的启动文件,可以在全局启动文件中加入类似以下的代码:
"<tt class="samp">if os.path.isfile('.pythonrc.py'):
execfile('.pythonrc.py')</tt>"。
如果你想要在某个脚本中使用启动文件,必须要在脚本中写入这样的语句:<P><div class="verbatim"><pre>
import os
filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):
execfile(filename)</pre></div><P><BR><HR><H4>Footnotes</H4><DL><DT><A NAME="foot132">... prompt.</A><A HREF="node4.html#tex2html1"><SUP>2.1</SUP></A></DT><DD>
A problem with the GNU Readline package may prevent this.
</DD><DT><A NAME="foot135">...DEL)就会取消当前输入,回到主命令行。</A><A HREF="node4.html#tex2html2"><SUP>2.2</SUP></A></DT><DD>GNU readline
包的一个错误可能会造成无法正常工作。</DD></DL><DIV CLASS="navigation"><div class='online-navigation'><p></p><hr /><table align="center" width="100%" cellpadding="0" cellspacing="2"><tr><td class='online-navigation'><a rel="prev" title="1. 开胃菜 Whetting Your" href="node3.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="3. Python简介 An Informal" href="node5.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="node3.html">1. 开胃菜 Whetting Your</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="node5.html">3. Python简介 An Informal</A></div></div><hr /></DIV><!--End of Navigation Panel--><ADDRESS>译者:刘鑫(march.liu AT gmail DOT com) 由:limodou转(limodou AT gmail DOT com)<br>See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -