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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 第12章 輸入/輸出 **目錄表** + 文件 + 使用文件 + 儲存器 + 儲存與取儲存 + 概括 在很多時候,你會想要讓你的程序與用戶(可能是你自己)交互。你會從用戶那里得到輸入,然后打印一些結果。我們可以分別使用`raw_input`和`print`語句來完成這些功能。對于輸出,你也可以使用多種多樣的`str`(字符串)類。例如,你能夠使用`rjust`方法來得到一個按一定寬度右對齊的字符串。利用`help(str)`獲得更多詳情。 另一個常用的輸入/輸出類型是處理文件。創建、讀和寫文件的能力是許多程序所必需的,我們將會在這章探索如何實現這些功能。 你可以通過創建一個`file`類的對象來打開一個文件,分別使用`file`類的`read`、`readline`或`write`方法來恰當地讀寫文件。對文件的讀寫能力依賴于你在打開文件時指定的模式。最后,當你完成對文件的操作的時候,你調用`close`方法來告訴Python我們完成了對文件的使用。 ``` #!/usr/bin/python # Filename: using_file.py poem = '''\ Programming is fun When the work is done if you wanna make your work also fun: ????????use Python! ''' f = file('poem.txt', 'w') # open for 'w'riting f.write(poem) # write text to file f.close() # close the file f = file('poem.txt') # if no mode is specified, 'r'ead mode is assumed by default while True: ????line = f.readline() ????if len(line) == 0: # Zero length indicates EOF ????????break ????print line, ????# Notice comma to avoid automatic newline added by Python f.close() # close the file ``` (源文件:[code/using_file.py](code/using_file.py)) ## 輸出 ``` $ python using_file.py Programming is fun When the work is done if you wanna make your work also fun: ????????use Python! ``` ## 它如何工作 首先,我們通過指明我們希望打開的文件和模式來創建一個`file`類的實例。模式可以為讀模式(`'r'`)、寫模式(`'w'`)或追加模式(`'a'`)。事實上還有多得多的模式可以使用,你可以使用`help(file)`來了解它們的詳情。 我們首先用寫模式打開文件,然后使用`file`類的`write`方法來寫文件,最后我們用`close`關閉這個文件。 接下來,我們再一次打開同一個文件來讀文件。如果我們沒有指定模式,讀模式會作為默認的模式。在一個循環中,我們使用`readline`方法讀文件的每一行。這個方法返回包括行末換行符的一個完整行。所以,當一個 空的 字符串被返回的時候,即表示文件末已經到達了,于是我們停止循環。 注意,因為從文件讀到的內容已經以換行符結尾,所以我們在`print`語句上使用逗號來消除自動換行。最后,我們用`close`關閉這個文件。 現在,來看一下`poem.txt`文件的內容來驗證程序確實工作正常了。
                  <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>

                              哎呀哎呀视频在线观看