<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 習題 15: 讀取文件 你已經學過了 raw_input 和 argv,這些是你開始學習讀取文件的必備基礎。你可能需要多多實驗才能明白它的工作原理,所以你要細心做練習,并且仔細檢查結果。處理文件需要非常仔細,如果不仔細的話,你可能會吧有用的文件弄壞或者清空。導致前功盡棄。 這節練習涉及到寫兩個文件。一個正常的 ex15.py 文件,另外一個是 ex15_sample.txt,第二個文件并不是腳本,而是供你的腳本讀取的文本文件。以下是后者的內容: ~~~ This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. ~~~ 我們要做的是把該文件用我們的腳本“打開(open)”,然后打印出來。然而把文件名ex15_sample.txt 寫死(hardcode)在代碼中不是一個好主意,這些信息應該是用戶輸入的才對。如果我們碰到其他文件要處理,寫死的文件名就會給你帶來麻煩了。我們的解決方案是使用 argv 和 raw_input 來從用戶獲取信息,從而知道哪些文件該被處理。 <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</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>from sys import argv&#13; &#13; script, filename = argv&#13; &#13; txt = open(filename)&#13; &#13; print "Here's your file %r:" % filename&#13; print txt.read()&#13; &#13; print "Type the filename again:"&#13; file_again = raw_input("&gt; ")&#13; &#13; txt_again = open(file_again)&#13; &#13; print txt_again.read()&#13; </pre> </div> </td> </tr></tbody></table> 這個腳本中有一些新奇的玩意,我們來快速地過一遍: 代碼的 1-3 行使用 argv 來獲取文件名,這個你應該已經熟悉了。接下來第 5 行我們看到 open 這個新命令。現在請在命令行運行 pydocopen 來讀讀它的說明。你可以看到它和你自己的腳本、或者 raw_input 命令類似,它會接受一個參數,并且返回一個值,你可以將這個值賦予一個變量。這就是你打開文件的過程。 第 7 行我們打印了一小行,但在第 8 行我們看到了新奇的東西。我們在 txt 上調用了一個函數。你從 open 獲得的東西是一個 file (文件),文件本身也支持一些命令。它接受命令的方式是使用句點 . (英文稱作 dot 或者 period),緊跟著你的命令,然后是類似 open 和 raw_input 一樣的參數。不同點是:當你說 txt.read 時,你的意思其實是:“嘿 txt!執行你的 read 命令,無需任何參數!” 腳本剩下的部分基本差不多,不過我就把剩下的分析作為加分習題留給你自己了。 ### 你應該看到的結果 我的腳本叫 “ex15_sample.txt”,以下是執行結果: ~~~ $ python ex15.py ex15_sample.txt Here's your file 'ex15_sample.txt': This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. Type the filename again: > ex15_sample.txt This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. $ ~~~ ### 加分習題 這節的難度跨越有點大,所以你要盡量做好這節加分習題,然后再繼續后面的章節。 1. 在每一行的上面用注解說明這一行的用途。 1. 如果你不確定答案,就問別人,或者上網搜索。大部分時候,只要搜索 “python” 加上你要搜的東西就能得到你要的答案。比如搜索一下“python open”。 1. 我使用了“命令”這個詞,不過實際上它們的名字是“函數(function)”和“方法(method)。上網搜索一下這兩者的意義和區別。看不明白也沒關系,迷失在別的程序員的知識海洋里是很正常的一件事情。 1. 刪掉 10-15 行使用到 raw_input 的部分,再運行一遍腳本。 1. 只是用 raw_input 寫這個腳本,想想那種得到文件名稱的方法更好,以及為什么。 1. 運行 pydocfile 向下滾動直到看見 read() 命令(函數/方法)。看到很多別的命令了吧,你可以找幾條試試看。不需要看那些包含 __ (兩個下劃線)的命令,這些只是垃圾而已。 1. 再次運行 python 在命令行下使用 open 打開一個文件,這種 open 和 read 的方法也值得你一學。 1. 讓你的腳本針對 txt and txt_again 變量執行一下 close() ,處理完文件后你需要將其關閉,這是很重要的一點。
                  <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>

                              哎呀哎呀视频在线观看