<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國際加速解決方案。 廣告
                [TOC] <br> ### os模塊介紹 os模塊主要用于程序與操作系統之間的交互。 在os.py源碼中,有一段說明很重要,如下: ```python r"""OS routines for NT or Posix depending on what system we're on. This exports: - all functions from posix, nt or ce, e.g. unlink, stat, etc. - os.path is either posixpath or ntpath - os.name is either 'posix', 'nt' or 'ce'. - os.curdir is a string representing the current directory ('.' or ':') - os.pardir is a string representing the parent directory ('..' or '::') - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\') - os.extsep is the extension separator (always '.') - os.altsep is the alternate pathname separator (None or '/') - os.pathsep is the component separator used in $PATH etc - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n') - os.defpath is the default search path for executables - os.devnull is the file path of the null device ('/dev/null', etc.) Programs that import and use 'os' stand a better chance of being portable between different platforms. Of course, they must then only use functions that are defined by all platforms (e.g., unlink and opendir), and leave all pathname manipulation to os.path (e.g., split and join). """ ``` 從這段描述可知,在python我們使用相同的os模塊與不同的平臺,如windows或linux交互。 os模塊中有非常多的變量和方法,這里就不一一介紹,還是那句話,`源碼才是最好的學習資料`。 ### os 模塊常用變量 ```python >>> os.name # 顯示當前系統名稱,win->'nt'; Linux->'posix' 'nt' >>> os.sep # 顯示當前系統中,路徑分割符 '\\' >>> os.linesep # 顯示當前系統的文本換行符 '\r\n' >>> os.environ # 顯示系統環境變量 ``` ### os 模塊常用方法 ```python >>> os.getcwd() # 注意:是當前運行腳本的路徑,不是python腳本文件的路徑 'D:\\os_exec' # 與目錄相關的方法 >>> os.mkdir('tmp') # 創建文件夾tmp,如果tmp已存在,則會報錯哦 >>> os.rmdir('tmp') # 刪除空文件夾,如果文件夾不為空或不存在,都會報錯哦 >>> os.makedirs('tmp4/tmp44') # 創建多級文件夾tmp4/tmp44 >>> os.removedirs('tmp4/tmp44') # 若目錄為空,則刪除,并遞歸到上一級目錄,如若也為空,則刪除,依此類推 >>> os.rename('src','dst')# 修改文件(夾)名稱,從src修改為dst >>> os.listdir() # 返回目錄下的所有文件(夾)列表 ['dst', 'tmp1', 'tmp2', 'tmp5', '新建文本文檔.txt'] # 與調用系統命令有關 >>> os.system('dir') # 在子shell中運行shell命令,直接顯示,返回值0表示成功,1表示失敗 # 與文件相關的方法 >>> os.remove('tmp.txt') # 刪除文件 ``` **刪除非空文件夾:** 有心的朋友或許已經發現,在刪除文件夾時,如果文件夾非空,則用os是無法刪除的。此時可以使用`shutil`庫,該庫為python內置庫,是一個對文件及文件夾高級操作的庫,可以與os庫互補完成一些操作,如文件夾的整體復制,移動文件夾,對文件重命名等。 ```python shutil.rmtree(path) #遞歸刪除文件夾 ``` ### os.path 模塊常用方法 ```python >>> os.path.abspath("tmp1") # 返回path規范化的絕對路徑 'D:\\os_exec\\tmp1' >>> os.path.split("D://os_exec/test.txt") # 將path分割成目錄和文件名二元組返回 ('D://os_exec', 'test.txt') >>> os.path.dirname("D://os_exec/test.txt") # 返回path的目錄,同os.path.split(path)的第一個元素 'D://os_exec' >>> os.path.basename("D://os_exec/test.txt") # 返回path最后的文件名(如果path以/或\結尾則返回空值)同os.path.split(path)的第二個元素 'test.txt' >>> os.path.exists("D://os_exec/test.txt") # 判斷path是否存在 True >>> os.path.isfile("D://os_exec/test.txt") # 判斷path是否文件 True >>> os.path.isdir("D://os_exec/test.txt") # 判斷path是否文件夾 False >>> os.path.join("D:\\os_exec","tmp","test.txt") # 將多個路徑組合后返回 'D:\\os_exec\\tmp\\test.txt' >>> os.path.getatime('D:\\os_exec\\test.txt') # 獲取path新增時間 1530950642.685528 ``` <hr style="margin-top:100px"> :-: ![](https://box.kancloud.cn/2ff0bc02ec938fef8b6dd7b7f16ee11d_258x258.jpg) ***微信掃一掃,關注“python測試開發圈”,了解更多測試教程!***
                  <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>

                              哎呀哎呀视频在线观看