python os模組筆記

2021-08-09 08:02:40 字數 1436 閱讀 5192

# os 模組

os.sep 可以取代作業系統特定的路徑分隔符。windows下為 '\\'

os.name 字串指示你正在使用的平台。比如對於windows,它是'nt',而對於linux/unix使用者,它是 'posix'

os.getcwd() 函式得到當前工作目錄,即當前python指令碼工作的目錄路徑  

os.getenv() 獲取乙個環境變數,如果沒有返回none  

os.putenv(key, value) 設定乙個環境變數值  

os.listdir(path) 返回指定目錄下的所有檔案和目錄名  

os.remove(path) 函式用來刪除乙個檔案  

os.system(command) 函式用來執行shell命令  

os.linesep 字串給出當前平台使用的行終止符。例如,windows使用 '\r\n',linux使用 '\n' 而mac使用 '\r'

os.path.split(path)  函式返回乙個路徑的目錄名和檔名  

os.path.isfile() 和os.path.isdir()函式分別檢驗給出的路徑是乙個檔案還是目錄  

os.path.exists() 函式用來檢驗給出的路徑是否真地存在  

os.curdir  返回當前目錄 ('.')  

os.mkdir(path) 建立乙個目錄  

os.makedirs(path) 遞迴的建立目錄  

os.chdir(dirname) 改變工作目錄到dirname      

os.path.getsize(name) 獲得檔案大小,如果name是目錄返回0l

os.path.abspath(name) 獲得絕對路徑  

os.path.normpath(path) 規範path字串形式  

os.path.splitext()  分離檔名與副檔名  

os.path.join(path,name) 連線目錄與檔名或目錄  

os.path.basename(path) 返回檔名  

os.path.dirname(path) 返回檔案路徑  

os.walk(top,topdown=true,οnerrοr=none)  遍歷迭代目錄  

os.rename(src, dst)  重新命名file或者directory src到dst 如果dst是乙個存在的directory, 將丟擲oserror. 在unix, 如果dst在存且是乙個file, 如果使用者有許可權的話,它將被安靜的替換. 操作將會失敗在某些unix 中如果src和dst在不同的檔案系統中. 如果成功, 這命名操作將會是乙個原子操作 (這是posix 需要). 在 windows上, 如果dst已經存在, 將丟擲oserror,即使它是乙個檔案. 在unix,windows中有效。  

os.renames(old, new) 遞迴重新命名資料夾或者檔案。像rename()  

python os介紹 Python os模組介紹

os模組主要用於執行系統命令 import os os.remname file.txt file1.txt 檔案重新命名 os.remove file1.txt 刪除檔案 os.mkdir test 建立資料夾 os.rmdir test 刪除資料夾 os.sep 可以取代作業系統特定的路徑分割符...

python os模組總結

在python的標準庫os模組中包含普遍的作業系統功能。程式能夠與平台,就靠這個模組了。下面是os模組常用的方法.1.os.sep 可以取代作業系統特定的路徑分割符 2.os.name 字串指示你正在使用的平台。比如對於windows,它是 nt 而對於linux unix使用者,它是 posix ...

python os模組學習

os模組提供了與作業系統打交道時常用的功能實現,換句話說,要是你想讓你的 跑在不同的操作平台上,這個模組是不可以不掌握的。一.常用方法 1.os.getcwd 返回當前的工作目錄 import os print os.getcwd c users tamarous documents visual ...