<pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
    <pre id="vvttv"></pre>

      <p id="vvttv"></p>

          <p id="vvttv"></p>

                <p id="vvttv"></p>

                <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                  <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                    <p id="vvttv"></p>

                    習題 15: 讀取文件?

                    你已經學過了 raw_inputargv,這些是你開始學習讀取文件的必備基礎。你可能需要多多實驗才能明白它的工作原理,所以你要細心做練習,并且仔細檢查結果。處理文件需要非常仔細,如果不仔細的話,你可能會吧有用的文件弄壞或者清空。導致前功盡棄。

                    這節練習涉及到寫兩個文件。一個正常的 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)在代碼中不是一個好主意,這些信息應該是用戶輸入的才對。如果我們碰到其他文件要處理,寫死的文件名就會給你帶來麻煩了。我們的解決方案是使用 argvraw_input 來從用戶獲取信息,從而知道哪些文件該被處理。

                     1
                     2
                     3
                     4
                     5
                     6
                     7
                     8
                     9
                    10
                    11
                    12
                    13
                    14
                    15
                    from sys import argv
                    
                    script, filename = argv
                    
                    txt = open(filename)
                    
                    print "Here's your file %r:" % filename
                    print txt.read()
                    
                    print "Type the filename again:"
                    file_again = raw_input("> ")
                    
                    txt_again = open(file_again)
                    
                    print txt_again.read()
                    

                    這個腳本中有一些新奇的玩意,我們來快速地過一遍:

                    代碼的 1-3 行使用 argv 來獲取文件名,這個你應該已經熟悉了。接下來第 5 行我們看到 open 這個新命令。現在請在命令行運行 pydoc open 來讀讀它的說明。你可以看到它和你自己的腳本、或者 raw_input 命令類似,它會接受一個參數,并且返回一個值,你可以將這個值賦予一個變量。這就是你打開文件的過程。

                    第 7 行我們打印了一小行,但在第 8 行我們看到了新奇的東西。我們在 txt 上調用了一個函數。你從 open 獲得的東西是一個 file (文件),文件本身也支持一些命令。它接受命令的方式是使用句點 . (英文稱作 dot 或者 period),緊跟著你的命令,然后是類似 openraw_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. 在每一行的上面用注解說明這一行的用途。
                    2. 如果你不確定答案,就問別人,或者上網搜索。大部分時候,只要搜索 “python” 加上你要搜的東西就能得到你要的答案。比如搜索一下“python open”。
                    3. 我使用了“命令”這個詞,不過實際上它們的名字是“函數(function)”和“方法(method)。上網搜索一下這兩者的意義和區別。看不明白也沒關系,迷失在別的程序員的知識海洋里是很正常的一件事情。
                    4. 刪掉 10-15 行使用到 raw_input 的部分,再運行一遍腳本。
                    5. 只是用 raw_input 寫這個腳本,想想那種得到文件名稱的方法更好,以及為什么。
                    6. 運行 pydoc file 向下滾動直到看見 read() 命令(函數/方法)。看到很多別的命令了吧,你可以找幾條試試看。不需要看那些包含 __ (兩個下劃線)的命令,這些只是垃圾而已。
                    7. 再次運行 python 在命令行下使用 open 打開一個文件,這種 open 和 read 的方法也值得你一學。
                    8. 讓你的腳本針對 txt and txt_again 變量執行一下 close() ,處理完文件后你需要將其關閉,這是很重要的一點。

                    Project Versions

                    Table Of Contents

                    Previous topic

                    習題 14: 提示和傳遞

                    Next topic

                    習題 16: 讀寫文件

                    This Page

                      <pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
                      <pre id="vvttv"></pre>

                        <p id="vvttv"></p>

                            <p id="vvttv"></p>

                                  <p id="vvttv"></p>

                                  <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                                    <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                                      <p id="vvttv"></p>

                                      这里只有精品视频