⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 93.html

📁 国外python经典教材,python爱好者的首选
💻 HTML
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -&gt; Standard Input, Output, and Error</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> &gt; <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> &gt; <a href="89.html" class="navtitle">9. Input and Output</a> &gt; <span class="nonavtitle">Standard Input, Output, and Error</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="92.html" title="Files"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=93" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="93.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="94.html" title="The print Statement"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+9%3A02%3A59+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238111161105084124006204201004115</font><a href="read6.asp?bookname=0735710910&snode=93&now=5%2F28%2F2002+9%3A02%3A59+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Standard Input, Output, and Error</h3>
<p>The interpreter provides three standard file objects, known as <i>standard input, standard output,</I> and <I>standard error,</I> which are available in the <Tt claSS="monofont">sys</TT>  module as <tt clASS="monofont">sys.stdin</Tt>, <tt class="monofont">sys.stdout</tt>, and <tt class="monofont">sys.stderr</tt>, respectively. <tT clAss="monofont">stdin</tT>  is a file object corresponding to the stream of input characters supplied to the interpreter. <tt clAss="monofont">stdout</tT>  is the file object that receives output produced by <TT Class="monofont">print</TT>. <TT clasS="monofont">stderr</TT>  is a file that receives error messages. More often than not, <Tt claSS="monofont">stdin</TT>  is mapped to the user抯 keyboard, while <tt class="monofont">stdout</tt>  and <tt class="monofont">stderr</tt>  produce text onscreen.</p>

<p>The methods described in the preceding section can be used to perform raw I/O with the user. For example, the following function reads a line of input from standard input:</P>

<prE>

def gets(): 
    text = "" 
    while 1: 
       c = sys.stdin.read(1) 
       text = text + c 
       if c == '\n': break 
    return text </pre>

<P>Alternatively, the built-in function <tt clAss="monofont">raw_input(</tT><I><TT clasS="monofont">prompt</TT></I>
<tt clASS="monofont">)</Tt> can read a line of text from <tt cLASS="monofont">stdin</tt>:</p>

<pre>

s = raw_input("type something : ") 
print "You typed '%s'" % (s,) </pre>

<p>Finally, keyboard interrupts (often generated by Ctrl+C) result in a <tt class="monofont">KeyboardInterrupt</tt>  exception that can be caught using an exception handler.</p>

<P>If necessary, the values of <tt ClasS="monofont">sys.stdout</tt>, <tt ClasS="monofont">sys.stdin</TT>, and <Tt claSS="monofont">sys.stderr</TT>  can be replaced with other file objects, in which case the <tt clASS="monofont">print</Tt>  statement and raw input functions use the new values. The original values of <tt cLASS="monofont">sys.stdout</tt>, <tt class="monofont">sys.stdin</tt>, and <tt class="monofont">sys.stderr</tt>  at interpreter startup are also available in <Tt cLass="monofont">sys._ _stdout_ _</Tt>, <tt cLass="monofont">sys._ _stdin_ _</TT>, and <TT clasS="monofont">sys._ _stderr_ _</TT>, respectively.</P>

<p>Note that in some cases <tt cLASS="monofont">sys.stdin</tt>, <tt CLASs="monofont">sys.stdout</tt>, and <tt class="monofont">sys.stderr</tt>  may be altered by the use of an integrated development environment (IDE). For example, when running Python under Idle, <tt class="monofont">sys.stdin</tT>  is replaced with an object that behaves like a file, but is really an object in the development environment. In this case, certain low-level methods such as <tt ClasS="monofont">read()</tt> and <tt ClasS="monofont">seek()</TT> may be unavailable.</P>
</font>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 3/28/2002<br>Python Essential Reference, Second Edition, &copy;&nbsp;2002 New Riders Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="92.html" title="Files"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=93" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="93.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="94.html" title="The print Statement"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
</TD></TR></TABLE>




<!--EndOfBrowse-->

</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -