<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 問題 你想像一個文件中寫入數據,但是前提必須是這個文件在文件系統上不存在。也就是不允許覆蓋已存在的文件內容。 ## 解決方案 可以在 `open()` 函數中使用x模式來代替w模式的方法來解決這個問題。比如: >>> with open('somefile', 'wt') as f: ... f.write('Hello\n') ... >>> with open('somefile', 'xt') as f: ... f.write('Hello\n') ... Traceback (most recent call last): File "<stdin>", line 1, in <module> FileExistsError: [Errno 17] File exists: 'somefile' >>> 如果文件是二進制的,使用 `xb` 來代替 `xt` 討論 這一小節演示了在寫文件時通常會遇到的一個問題的完美解決方案(不小心覆蓋一個已存在的文件)。 一個替代方案是先測試這個文件是否存在,像下面這樣: ~~~ >>> import os >>> if not os.path.exists('somefile'): ... with open('somefile', 'wt') as f: ... f.write('Hello\n') ... else: ... print('File already exists!') ... File already exists! >>> ~~~ 顯而易見,使用x文件模式更加簡單。要注意的是x模式是一個Python3對 `open()` 函數特有的擴展。在Python的舊版本或者是Python實現的底層C函數庫中都是沒有這個模式的。
                  <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>

                              哎呀哎呀视频在线观看