Python中的OS標準庫學習

2021-10-10 14:22:34 字數 1281 閱讀 4548

**

一、基本介紹

os庫提供通用的,基本的作業系統互動功能

路徑操作:os.path子庫,處理檔案路徑及資訊

程序管理:啟動系統中其他程式

環境引數:獲得系統軟硬體資訊等環境引數

二、常用命令

os.path子庫

指令用途

os.path.normpath(path)

歸一化path的表示形式,統一用\分隔路徑

os.path.abspath(path)

返回path在當前系統中的絕對路徑

os.path.relpath(path)

返回當前程式與檔案之間的相對路徑

os.path.dirname(path)

返回path中的目錄名稱

os.path.basename(path)

返回path中最後的檔名稱

os.path.join(path,*path)

組合path與*path,返回乙個路徑字串

os.path.exists(path)

判斷path對應檔案或目錄是否存在,返回true或false

os.path.isfile(path)

判斷path所對應是否為已存在的檔案,返回true或false

os.path.isdir(path)

判斷path對應是否為已存在的目錄,返回true或false

os.path.getatime(path)

返回path對應檔案或目錄上一次的訪問時間

os.path.getmtime(path)

返回path對應檔案或目錄最近一次的修改時間

os.path.getctime(path)

返回path對應檔案或目錄的建立時間

os.path.getsize(path)

返回path對應檔案的大小,以位元組為單位

程序管理指令

指令用途

os.system(command)

-執行程式或命令command(在windows中,返回值為cmd的呼叫返回資訊

os.chdir(path)

返回當前程式操作的路徑

os.getcwd()

返回程式的當前目錄

os.getlogin()

獲得當前系統登入使用者名稱

os.cpu_count()

獲得當前系統的cpu數量

os.urandom(n)

獲得n個位元組的隨機字串,通常用於加密或解密運算

python標準庫os模組學習

encoding utf 8 import os import sys 分離檔案與目錄位址 currdir os.path.split sys.argv 0 0 迴圈建立檔案 for i in range 0,10 filename s.txt i print filename open filen...

python標準庫 os庫

os模組主要用於跟作業系統打交道 os模組常用的方法 os.getcwd 獲取當前工作目錄,即當前python指令碼工作的目錄路徑 os.chdir dirname 改變當前指令碼工作目錄 相當於shell下cd os.curdir 返回當前目錄 os.pardir 獲取當前目錄的父目錄字串名 os...

python標準庫之 os

常用的python標準庫有 2 time datatime 時間與日期相關 3 科學計算相關 math 4 網路請求相關 urline os模組主要是對檔案和目錄的操作,常用的方法 os.listdir 列出當前目錄下有哪些檔案和目錄,以列表形式返回。可以通過遍歷這個列表,去操作我們想要操作的檔案 ...