python os模組

來源:互聯網
上載者:User

標籤:file   and   .com   blog   ref   合成   刪除空目錄   post   com   

python os模組常用方法:

os.getcwd() 擷取目前的目錄,和shell中的pwd類似。os.chdir(‘dirname‘) 切換目錄 和shell中的cd類似。os.makedirs(‘dirname1/dirname2‘)  級聯建立目錄 和shell中的mkdir -p 類似os.removedirs(‘dirname1‘) 若目錄為空白,則刪除,並遞迴到上一級目錄,上一級目錄如果也為空白,則刪除,依次類推。os.mkdir(‘dirname‘) 建立目錄,和shell中的mkdir類似os.rmdir(‘dirname‘) 刪除空目錄,和shell中的rmdir命令類似os.listdir(‘dirname‘) 列出指定目錄下的所有檔案和子目錄,包括隱藏檔案,以列表的方式列印。和shell中的ls -al 類似。os.remove (‘filename‘) 刪除一個檔案os.rename(‘oldname‘,‘newname‘)  重新命名檔案或目錄os.stat(‘path/filename‘) 擷取檔案或目錄資訊os.name 輸出當前使用的平台。win ==> ‘nt‘ , linux為‘posix‘os.system(‘bash command‘) 運行shell命令,直接顯示os.environ 擷取系統內容變數os.path.abspath(‘path‘) 返回path的絕對路徑os.path.split(path) 將path分割成目錄和檔案名稱以元祖的形式返回os.path.dirname(path) 返回path目錄os.path.basename(path) 返回path最後的檔案名稱。os.path.exists(path) 如果path存在,返回True.如果path不存在,返回Flase.os.access(‘path‘,os.F_OK) 是否存在os.access(‘path‘,os.R_OK) 是否可讀os.access(‘path‘,os.W_OK) 是否可寫os.access(‘path‘,os.X_OK) 是否可執行os.path.isabs(path) 如果path是絕對路徑,返回True.os.path.isfile(path) 如果path是一個存在的檔案,則返回True.os.path.isdir(path) 如果path是一個存在的目錄,怎返回True.os.path.join(path1,path2) 將path1和path2組合成一個路徑os.path.getatime(path) 返回path所指向的檔案或者目錄最後存取時間os.path.getmtime (path) 返回path所指向的檔案或者目錄的最後修改時間

  

參考:https://www.cnblogs.com/gide/p/5570886.html

 

python os模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.