Python os庫常用速查

2021-08-16 23:42:31 字數 510 閱讀 9660

記錄一些常用方法

os.path.dirname(file) # 獲取當前檔案所在路徑 用於獲取路徑

os.getcwd() # 得到當前工作目錄(類似於終端命令中的pwd)

os.path.join() # 用於連線檔案位址和檔名

os.listdir() # 獲取指定目錄下的所有檔案和目錄

os.remove() # 刪除指定檔案,相當於終端中的rm

os.rmdir() # 刪除指定目錄,相當於終端中的rm -r

os.mkdir() # 建立指定目錄

os.makedirs() # 可以遞迴式地建立多級子目錄

os.path.exists() # 檢驗指定的物件是否存在

os.path.split() # 分開返回路徑和檔名

os.system() # 執行終端的命令

os.chdir() # 改變目錄到指定目錄

python os庫常用函式學習

使用環境64位的fedora 18,python版本是python 2.7.3 1 os.getcwd 函式 功能 獲取當前目錄,python 的工作目 import os pwd os.getcwd print pwd 2 os.name 函式 功能 獲取當前使用的作業系統 獲取資訊不夠詳細 其中...

python os常用函式

1.os.path.isfile path 用來判斷傳遞的引數是否是檔案.2.os.listdir path 獲取引數當前目錄下的檔案和資料夾,不包含 和 3.os.remove 用於刪除指定路徑的檔案。如果指定的路徑是乙個目錄,將丟擲oserror 4.os.getcwd 獲取當前工作目錄路徑 5...

Python os模組常用方法

在讀檔案的時候往往需要遍歷資料夾,python的os.path包含了很多檔案 資料夾操作的方法。下面列出 os.path.abspath path 返回絕對路徑 os.path.basename path 返回檔名 os.path.commonprefix list 返回多個路徑中,所有path共有...