os庫:提供對操作系統進行操作的接口,在接口測試當中,最常用的是對目錄的處理
import os
print(dir(os))
#獲取當前文件目錄
print(os.getcwd())
#刪除文件夾
os.rmdir('D:/ostest')
#創建文件夾
os.mkdir('D:/ostest')
#文件重命名
os.rename('D:/ostest','D:/newostest')
#獲取當前文件目錄(獲取所引用的模塊所在的絕對路徑,__file__為內置屬性)
print('當前文件目錄:',os.path.dirname(__file__))
print('當前文件的上級目錄:',os.path.dirname(os.path.dirname(__file__)))
print('當前文件的上級目錄的上級目錄:',os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
#路徑的拼接(路徑拼接、文件內容打印在接口測試中經常要用到,比如讀取文件里的內容、把執行過程當中出現的錯誤信息寫到日志文件當中等等,都需要對目錄的處理)
base_dir = os.path.dirname(os.path.dirname(__file__))
print(base_dir)
file_dir = os.path.join(base_dir,'untitled4/login.py')
print(file_dir)
#打印文件內容
f = open(file_dir,'r',encoding='UTF-8')
print(f.read())
