標籤: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模組