<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 練習16.讀寫文件 如果你做了上一個練習的附加題部分,你應該已經了解了文件相關的各種命令(方法/函數)。下面的這張表,是你應該記住的命令: > - close -- 關閉文件。跟你編輯器的 文件->保存.. 一個意思。 > - read -- 讀取文件內容。你可以把結果賦給一個變量。 > - readline -- 讀取文本文件中的一行。 > - truncate -- 清空文件,請謹慎使用該命令。 > - write('stuff') -- 將stuff寫入文件。 這是你現在該知道的重要命令。有些命令需要接受參數,這對我們并不重要。你只要記住 `write` 需要接收一個字符串作為參數,從而將該字符串寫入文件。 讓我們來使用這些命令做一個簡單的文本編輯器吧: ~~~ from sys import argv script, filename = argv print "We're going to erase %r." % filename print "If you don't want that, hit CTRL-C (^C)." print "If you do want that, hit RETURN." raw_input("?") print "Opening the file..." target = open(filename, 'w') print "Truncating the file. Goodbye!" target.truncate() print "Now I'm going to ask you for three lines." line1 = raw_input("line 1: ") line2 = raw_input("line 2: ") line3 = raw_input("line 3: ") print "I'm going to write these to the file." target.write(line1) target.write("\n") target.write(line2) target.write("\n") target.write(line3) target.write("\n") print "And finally, we close it." target.close() ~~~ 這個文件是夠大的,大概是你創建過的最大的文件。所以慢慢來,仔細檢查,讓它能運行起來。有一個小技巧就是你可以讓你的腳本一部分一部分地運行起來。先寫 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: Mary had a little lamb line 2: Its fleece was white as snow line 3: It was also tasty 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 對于文件的寫入操作態度是安全第一,所以你只有特別指定以后,它才會進行寫入操作。 > 1. 如果你使用'w' 模式打開一個文件,那么還需要`target.truncate()`嗎?閱讀Python關于`open`函數的文檔, 看你理解的對不對。 ## 常見問題 ### Q:`truncate()`方法必須要有參數'w'嗎? > 參考附加題5 ### Q: 'w'參數是什么意思? > 它只是打開文件的一種模式。如果你用了這個參數,表示"以寫(`write`)模式打開文件。同樣有`'r'`表示只讀模式,`'a'`表示追加模式,還有一些其他的修飾符。 ### Q: 有什么修飾符我可以用在打開文件的模式上? > 最重要的一個就是`+`, 使用它,你可以有`'w+'`, `'r+'`, 和`'a+'`模式. 這樣可以同時以讀寫模式打開文件。 ### Q: `open(filename)`是以'r' (只讀) 模式打開文件嗎? > 是的,'r' 是 `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>

                              哎呀哎呀视频在线观看