📄 ch12.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css"><!--@import url(stylesheet/text.css);@import url(stylesheet/box.css);--></style><title>简明 Python 教程 / 输入/输出 / 文件 </title></head><body><table width="100%"><tr><th colspan="3" align="center"><span class="header">简明 Python 教程</span></th></tr><th colspan="3" align="center">第12章 输入/输出</th><tr><th width="20%" align="left"><a href="ch11s08.html">上一页</a></th><th width="60%" align="center"><span class="header2">文件</span></th><th align="right"><a href="ch12s02.html">下一页</a></th></tr></table><hr noshade><h1>第12章 输入/输出</h1><p><strong>目录表</strong></p><dl><dt><a href="#s01">文件</a></dt><dd><a href="ch12.html#using">使用文件</a></dd><dt><a href="ch12s02.html">储存器</a></dt><dd><a href="ch12s02.html#pickling">储存与取储存</a></dd><dt><a href="ch12s03.html">概括</a></dt></dl><p>在很多时候,你会想要让你的程序与用户(可能是你自己)交互。你会从用户那里得到输入,然后打印一些结果。我们可以分别使用<code>raw_input</code>和<code>print</code>语句来完成这些功能。对于输出,你也可以使用多种多样的<code>str</code>(字符串)类。例如,你能够使用<code>rjust</code>方法来得到一个按一定宽度右对齐的字符串。利用<code>help(str)</code>获得更多详情。</p><p>另一个常用的输入/输出类型是处理文件。创建、读和写文件的能力是许多程序所必需的,我们将会在这章探索如何实现这些功能。</p><h1><a name="s01">文件</a></h1><p>你可以通过创建一个<code>file</code>类的对象来打开一个文件,分别使用<code>file</code>类的<code>read</code>、<code>readline</code>或<code>write</code>方法来恰当地读写文件。对文件的读写能力依赖于你在打开文件时指定的模式。最后,当你完成对文件的操作的时候,你调用<code>close</code>方法来告诉Python我们完成了对文件的使用。</p><h2><a name="using">使用文件</a></h2><p class="exampletitle"><a name="e121">例12.1 使用文件</a></p><p class="filebox"><code class="comment">#!/usr/bin/python<br># Filename: using_file.py</code><br><br><code>poem = </code><code class="cite">'''\<br>Programming is fun<br>When the work is done<br>if you wanna make your work also fun:<br> use Python!<br>'''</code><br><br><code>f = </code><code class="func">file</code><code>(</code><code class="cite">'poem.txt'</code><code>, </code><code class="cite">'w'</code><code>) </code><code class="comment"># open for 'w'riting</code><br><code>f.write(poem) </code><code class="comment"># write text to file</code><br><code>f.close() </code><code class="comment"># close the file</code><br><br><code>f = </code><code class="func">file</code><code>(</code><code class="cite">'poem.txt'</code><code>)</code><br><code class="comment"># if no mode is specified, 'r'ead mode is assumed by default</code><br><code class="key">while </code><code class="func">True</code><code>:<br> line = f.readline()</code><br><code class="key"> if </code><code class="func">len</code><code>(line) == </code><code class="cite">0</code><code>: </code><code class="comment"># Zero length indicates EOF</code><br><code class="key"> break</code><br><code class="key"> print </code><code>line,</code><br><code class="comment"> # Notice comma to avoid automatic newline added by Python</code><br><code>f.close() </code><code class="comment"># close the file</code></p><p>(源文件:<a href="code/using_file.py">code/using_file.py</a>)</p><h2>输出</h2><p class="codebox"><code>$ python using_file.py<br>Programming is fun<br>When the work is done<br>if you wanna make your work also fun:<br> use Python!</code></p><h2>它如何工作</h2><p>首先,我们通过指明我们希望打开的文件和模式来创建一个<code>file</code>类的实例。模式可以为读模式(<code>'r'</code>)、写模式(<code>'w'</code>)或追加模式(<code>'a'</code>)。事实上还有多得多的模式可以使用,你可以使用<code>help(file)</code>来了解它们的详情。</p><p>我们首先用写模式打开文件,然后使用<code>file</code>类的<code>write</code>方法来写文件,最后我们用<code>close</code>关闭这个文件。</p><p>接下来,我们再一次打开同一个文件来读文件。如果我们没有指定模式,读模式会作为默认的模式。在一个循环中,我们使用<code>readline</code>方法读文件的每一行。这个方法返回包括行末换行符的一个完整行。所以,当一个 <dfn>空的</dfn> 字符串被返回的时候,即表示文件末已经到达了,于是我们停止循环。</p><p>注意,因为从文件读到的内容已经以换行符结尾,所以我们在<code>print</code>语句上使用逗号来消除自动换行。最后,我们用<code>close</code>关闭这个文件。</p><p>现在,来看一下<code>poem.txt</code>文件的内容来验证程序确实工作正常了。</p><hr noshade><table width="100%"><tr><th width="20%" align="left"><a href="ch11s08.html">上一页</a></th><th width="60%" align="center"><a href="ch12.html">上一级</a></th><th width="20%" align="right"><a href="ch12s02.html">下一页</a></th></tr><tr><th width="20%" align="left">概括</th><th width="60%" align="center"><a href="index.html">首页</a></th><th align="right">储存器</th></tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -