os庫提供通用的、基本的操做系統交互功能dom
經常使用路徑操做、進程管理、環境參數等幾類函數
環境參數:得到系統軟硬件信息等環境參數spa
os.path子庫以path爲入口,用於操做和處理文件路徑操作系統
import os.path
或import os.path as op
3d
函數 | 描述 |
---|---|
os.path.abspath(path) | 返回path在當前系統中的絕對路徑,os.path.abspath("file.txt") # 'C:\\Users\\Tian Song\\Python36-32\\file.txt' |
os.path.normpath(path) | 歸一化path的表示形式,統一用\分隔路徑,os.path.normpath("D://PYE//file.txt") # 'D:\\PYE\\file.txt' |
os.path.relpath(path) | 返回當前程序與文件之間的相對路徑 (relative path),os.path.relpath("C://PYE//file.txt") # '..\\..\\..\\..\\..\\..\\..\\PYE\\file.txt' |
os.path.dirname(path) | 返回path中的目錄名稱,os.path.dirname("D://PYE//file.txt") # 'D://PYE' |
os.path.basename(path) | 返回path中最後的文件名稱,os.path.basename("D://PYE//file.txt") # 'file.txt' |
os.path.join(path, *paths) | 組合path與paths,返回一個路徑字符串,os.path.join("D:/", "PYE/file.txt") # 'D:/PYE/file.txt' |
os.path.exists(path) | 判斷path對應文件或目錄是否存在,返回True或False,os.path.exists("D://PYE//file.txt") # False |
os.path.isfile(path) | 判斷path所對應是否爲已存在的文件,返回True或False,os.path.isfile("D://PYE//file.txt") # True |
os.path.isdir(path) | 判斷path所對應是否爲已存在的目錄,返回True或False,os.path.isdir("D://PYE//file.txt") # False |
os.path.getatime(path) | 返回path對應文件或目錄上一次的訪問時間,os.path.getatime("D:/PYE/file.txt") # 1518356633.7551725 |
os.path.getmtime(path) | 返回path對應文件或目錄最近一次的修改時間,os.path.getmtime("D:/PYE/file.txt") # 1518356633.7551725 |
os.path.getctime(path) | 返回path對應文件或目錄的建立時間,time.ctime(os.path.getctime("D:/PYE/file.txt")) # 'Sun Feb 11 21:43:53 2018' |
os.path.getsize(path) | 返回path對應文件的大小,以字節爲單位,os.path.getsize("D:/PYE/file.txt") # 180768 |
os.path.abspath(path) os.path.normpath(path) os.path.relpath(path) os.path.dirname(path) os.path.basename(path) os.path.join(path) os.path.exists(path) os.path.isfile(path) os.path.isdir(path) os.path.getatime(path) os.path.getmtime(path) os.path.getctime(path) os.path.getsize(path)
os.system(command)
code
import os os.system("C:\\Windows\\System32\\calc.exe") # 0
獲取或改變系統環境信息orm
函數 | 描述 |
---|---|
os.chdir(path) | 修改當前程序操做的路徑,os.chdir("D:") |
os.getcwd() | 返回程序的當前路徑,os.getcwd() # 'D:\\' |
os.getlogin() | 得到當前系統登陸用戶名稱,os.getlogin() # 'Tian Song' |
os.cpu_count() | 得到當前系統的CPU數量,os.cpu_count() # 8 |
os.urandom(n) | 得到n個字節長度的隨機字符串,一般用於加解密運算,os.urandom(10) # b'7\xbe\xf2!\xc1=\x01gL\xb3' |