擅长:python、mysql、java
<p>当程序到达文件结尾时首先终止它。在</p>
<ol>
<li><p>使用open()打开文件</p>
<p>fp=打开(“样本.txt“,”r“)</p></li>
<li><p>使用for循环读取行</p>
<p>对于fp中的l:</p></li>
<li>打印线条</li>
</ol>
<p>fp=打开(“示例.txt“,”r“)</p>
<p>对于fp中的l:</p>
<pre><code>print (l,end='')
</code></pre>
<p>如果你正在使用python2.7</p>
<p>fp=打开(“示例.txt“,”r“)</p>
<p>对于fp中的l:</p>
^{pr2}$
<p>现在我们可以在按下crtl+D时使用异常处理来终止输入标准。这里我正在指定示例程序</p>
<p>尝试:</p>
<pre><code>str1=raw_input("Enter the String :")
print "Given String :",str1
</code></pre>
<p>除了:</p>
<pre><code>print "User typed ctrl+d"
</code></pre>