<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 習題 16: 讀寫文件 如果你做了上一個練習的加分習題,你應該已經了解了各種文件相關的命令(方法/函數)。你應該記住的命令如下: - close – 關閉文件。跟你編輯器的 文件->保存.. 一個意思。 - read – 讀取文件內容。你可以把結果賦給一個變量。 - readline – 讀取文本文件中的一行。 - truncate – 清空文件,請小心使用該命令。 - write(stuff) – 將stuff寫入文件。 這是你現在該知道的重要命令。有些命令需要接受參數,這對我們并不重要。你只要記住 write 的用法就可以了。 write 需要接收一個字符串作為參數,從而將該字符串寫入文件。 讓我們來使用這些命令做一個簡單的文本編輯器吧: <table class="highlighttable"><tbody><tr><td class="linenos"> <div class="linenodiv"> <pre> 1&#13; 2&#13; 3&#13; 4&#13; 5&#13; 6&#13; 7&#13; 8&#13; 9&#13; 10&#13; 11&#13; 12&#13; 13&#13; 14&#13; 15&#13; 16&#13; 17&#13; 18&#13; 19&#13; 20&#13; 21&#13; 22&#13; 23&#13; 24&#13; 25&#13; 26&#13; 27&#13; 28&#13; 29&#13; 30&#13; 31&#13; 32&#13; 33</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>from sys import argv&#13; &#13; script, filename = argv&#13; &#13; print "We're going to erase %r." % filename&#13; print "If you don't want that, hit CTRL-C (^C)."&#13; print "If you do want that, hit RETURN."&#13; &#13; raw_input("?")&#13; &#13; print "Opening the file..."&#13; target = open(filename, 'w')&#13; &#13; print "Truncating the file. Goodbye!"&#13; target.truncate()&#13; &#13; print "Now I'm going to ask you for three lines."&#13; &#13; line1 = raw_input("line 1: ")&#13; line2 = raw_input("line 2: ")&#13; line3 = raw_input("line 3: ")&#13; &#13; print "I'm going to write these to the file."&#13; &#13; target.write(line1)&#13; target.write("\n")&#13; target.write(line2)&#13; target.write("\n")&#13; target.write(line3)&#13; target.write("\n")&#13; &#13; print "And finally, we close it."&#13; target.close()&#13; </pre> </div> </td> </tr></tbody></table> 這個文件是夠大的,大概是你鍵入過的最大的文件。所以慢慢來,仔細檢查,讓它能運行起來。有一個小技巧就是你可以讓你的腳本一部分一部分地運行起來。先寫 1-8 行,讓它運行起來,再多運行 5 行,再接著多運行幾行,以此類推,直到整個腳本運行起來為止。 ### 你應該看到的結果 你將看到兩樣東西,一樣是你新腳本的輸出: ~~~ $ python ex16.py test.txt We're going to erase 'test.txt'. If you don't want that, hit CTRL-C (^C). If you do want that, hit RETURN. ? Opening the file... Truncating the file. Goodbye! Now I'm going to ask you for three lines. line 1: To all the people out there. line 2: I say I don't like my hair. line 3: I need to shave it off. I'm going to write these to the file. And finally, we close it. $ ~~~ 接下來打開你新建的文件(我的是 test.txt )檢查一下里邊的內容,怎么樣,不錯吧? ### 加分習題 1. 如果你覺得自己沒有弄懂的話,用我們的老辦法,在每一行之前加上注解,為自己理清思路。就算不能理清思路,你也可以知道自己究竟具體哪里沒弄明白。 1. 寫一個和上一個練習類似的腳本,使用 read 和 argv 讀取你剛才新建的文件。 1. 文件中重復的地方太多了。試著用一個 target.write() 將 line1, line2, line3 打印出來,你可以使用字符串、格式化字符、以及轉義字符。 1. 找出為什么我們需要給 open 多賦予一個 'w' 參數。提示: open 對于文件的寫入操作態度是安全第一,所以你只有特別指定以后,它才會進行寫入操作。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看