UNIX用户已经对标准输入、标准输出和标准错误的概念熟悉了。这一节是为其它不熟悉的人准备的。

stdout 和 stderr)是建立在每个UNIX系统内的管道(pipe)。当你 print 某东西时,结果输出到 stdout 管道中;当你的程序崩溃并打印出调试信息时(象Python中的错误跟踪),结果输出到 stderr 管道中。通常这两个管道只与你正在工作的终端窗口相联,所以当一个程序打印输出时,你可以看到输出,并且当一个程序崩溃时,你可以看到调试信息。(如果你在一个基于窗口的Python IDE系统上工作,stdout 和 stderr


例 5.32. stdout 和 stderr



<tt>>>> </tt><span class="userinput"><span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">for</span> i <span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">in</span> range(3):</span>
<tt>...     </tt><span class="userinput"><span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">print</span> <span class="pystring" style="color: olive; background-color: white;">'Dive in'</span></span>             <a target=_blank target="_blank" name="kgp.stdio.1.1" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201713_651811c9d170f41451.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="1" border="0" width="12" height="12" style="border: none; max-width: 100%;" />
<span class="computeroutput" style="color: teal; background-color: white;">Dive in
Dive in
Dive in</span>
<tt>>>> </tt><span class="userinput"><span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">import</span> sys</span>
<tt>>>> </tt><span class="userinput"><span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">for</span> i <span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">in</span> range(3):</span>
<tt>...     </tt><span class="userinput">sys.stdout.write(<span class="pystring" style="color: olive; background-color: white;">'Dive in'</span>)</span> <a target=_blank target="_blank" name="kgp.stdio.1.2" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201714_651811ca1680016285.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="2" border="0" width="12" height="12" style="border: none; max-width: 100%;" />
<span class="computeroutput" style="color: teal; background-color: white;">Dive inDive inDive in</span>
<tt>>>> </tt><span class="userinput"><span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">for</span> i <span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">in</span> range(3):</span>
<tt>...     </tt><span class="userinput">sys.stderr.write(<span class="pystring" style="color: olive; background-color: white;">'Dive in'</span>)</span> <a target=_blank target="_blank" name="kgp.stdio.1.3" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201714_651811ca55e4f19911.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="3" border="0" width="12" height="12" style="border: none; max-width: 100%;" />
<span class="computeroutput" style="color: teal; background-color: white;">Dive inDive inDive in</span>




python stdin使用 python的stdin_类文件

正如我们在例 3.28中看到的,我们可以使用Python内置的 range

python stdin使用 python的stdin_python stdin使用_02

stdout 是一个类文件对象;调用它的 write 函数会打印出任何给出的字符串。事实上,这就是 print 函数真正所做的;它会在正打印的字符串后面加上回车换行符,并调用sys.stdout.write。

python stdin使用 python的stdin_Python_03

stdout 和 stderr 将它们的输出发送到同一个地方:Python IDE,或终端(如果你正从命令行运行Python)。象 stdout,stderr


stdout 和 stderr 都是类文件对象,就象我们在提取输入源中所讨论的一样,但它们都是只写的。它们没有 read 方法,只有 write。然而,它们的确是类文件对象,并且你可以将任意文件对象或类文件对象赋给它们来重定向输出。


例 5.33. 重定向输出



<tt>[f8dy@oliver kgp]$ </tt><span class="userinput">python2 stdout.py</span>
<span class="computeroutput" style="color: teal; background-color: white;">Dive in</span>
<tt>[f8dy@oliver kgp]$ </tt><span class="userinput">cat out.log</span>
<span class="computeroutput" style="color: teal; background-color: white;">This message will be logged instead of displayed</span>



如果你还没有这样做,你可以下载本书中用到的本例和其它例子



<span class="pycomment" style="color: green; font-style: italic; background-color: white;">#stdout.py</span>
<span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">import</span> sys

<span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">print</span> <span class="pystring" style="color: olive; background-color: white;">'Dive in'</span>                                          <a target=_blank target="_blank" name="kgp.stdio.2.1" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201713_651811c9d170f41451.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="1" border="0" width="12" height="12" style="border: none; max-width: 100%;" />
saveout = sys.stdout                                     <a target=_blank target="_blank" name="kgp.stdio.2.2" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201714_651811ca1680016285.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="2" border="0" width="12" height="12" style="border: none; max-width: 100%;" />
fsock = open(<span class="pystring" style="color: olive; background-color: white;">'out.log'</span>, <span class="pystring" style="color: olive; background-color: white;">'w'</span>)                             <a target=_blank target="_blank" name="kgp.stdio.2.3" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201714_651811ca55e4f19911.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="3" border="0" width="12" height="12" style="border: none; max-width: 100%;" />
sys.stdout = fsock                                       <a target=_blank target="_blank" name="kgp.stdio.2.4" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201715_651811cb4ce834135.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="4" border="0" width="12" height="12" style="border: none; max-width: 100%;" /><span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">
print</span> <span class="pystring" style="color: olive; background-color: white;">'This message will be logged instead of displayed'</span> <a target=_blank target="_blank" name="kgp.stdio.2.5" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201716_651811cc7a8cd3848.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="5" border="0" width="12" height="12" style="border: none; max-width: 100%;" />
sys.stdout = saveout                                     <a target=_blank target="_blank" name="kgp.stdio.2.6" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201716_651811ccb9b847252.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="6" border="0" width="12" height="12" style="border: none; max-width: 100%;" />
fsock.close()                                            <a target=_blank target="_blank" name="kgp.stdio.2.7" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201717_651811cd0406652035.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="7" border="0" width="12" height="12" style="border: none; max-width: 100%;" />




python stdin使用 python的stdin_类文件

这样会打印到IDE的“交互窗口”中(或终端,如果你从命令行运行这一脚本)。

python stdin使用 python的stdin_python stdin使用_02

stdout

python stdin使用 python的stdin_Python_03

打开一个新文件用于写入。

python stdin使用 python的stdin_python stdin使用_07

将所有后续的输出重定向到我们刚打开的新文件上。

python stdin使用 python的stdin_类文件_08

这样只会将输出结果“打印”到日志文件中;在IDE窗口中或在屏幕上不会看到输出结果。

python stdin使用 python的stdin_类文件_09

stdout

python stdin使用 python的stdin_python stdin使用_10

关闭日志文件。


stderr 完全以相同的方式进行,用 sys.stderr 代替 sys.stdout。


例 5.34. 重定向错误信息



<tt>[f8dy@oliver kgp]$ </tt><span class="userinput">python2 stderr.py</span>
<tt>[f8dy@oliver kgp]$ </tt><span class="userinput">cat error.log</span>
<span class="computeroutput" style="color: teal; background-color: white;">Traceback (most recent line last):
  File "stderr.py", line 5, in ?
    raise Exception, 'this error will be logged'
Exception: this error will be logged</span>



如果你还没有这样做,你可以下载本书中用到的本例和其它例子



<span class="pycomment" style="color: green; font-style: italic; background-color: white;">#stderr.py</span>
<span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">import</span> sys

fsock = open(<span class="pystring" style="color: olive; background-color: white;">'error.log'</span>, <span class="pystring" style="color: olive; background-color: white;">'w'</span>)               <a target=_blank target="_blank" name="kgp.stdio.3.1" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201713_651811c9d170f41451.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="1" border="0" width="12" height="12" style="border: none; max-width: 100%;" />
sys.stderr = fsock                           <a target=_blank target="_blank" name="kgp.stdio.3.2" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201714_651811ca1680016285.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="2" border="0" width="12" height="12" style="border: none; max-width: 100%;" /><span class="pykeyword" style="font-weight: bold; color: navy; background-color: white;">
raise</span> Exception, <span class="pystring" style="color: olive; background-color: white;">'this error will be logged'</span> <a target=_blank target="_blank" name="kgp.stdio.3.3" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201714_651811ca55e4f19911.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="3" border="0" width="12" height="12" style="border: none; max-width: 100%;" /> <a target=_blank target="_blank" name="kgp.stdio.3.4" style="color: rgb(202, 0, 0);"></a><img src="https://s2.51cto.com/images/blog/202309/30201715_651811cb4ce834135.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="4" border="0" width="12" height="12" style="border: none; max-width: 100%;" />




python stdin使用 python的stdin_类文件

打开我们想用来存储调试信息的日志文件。

python stdin使用 python的stdin_python stdin使用_02

stderr

python stdin使用 python的stdin_Python_03

引发一个异常。从屏幕输出上我们可以注意到这样没有在屏幕上打印出任何东西。所以正常跟踪信息已经写进 error.log。

python stdin使用 python的stdin_python stdin使用_07

stderr 设回它的初始值。这样挺好,因为一旦程序崩溃(由于我们的异常),Python将替我们清理和关闭文件,并且 stderr 永远不恢复不会造成什么不同。因为,我提到过,一旦程序崩溃,则Python也结束。如果你希望在同一个脚本的后面去做其它的事情,恢复初始值对 stdout