在自動化測試中,經常需要查找操作文件,比如說查找配置文件(從而讀取配置文件的信息),查找測試報告(從而發送測試報告郵件),經常要對大量文件和大量路徑進行操作,這就依賴於os模塊,所以今天整理下比較常用的幾個方法。網上這方面資料也很多,每次整理,只是對自己所學的知識進行梳理,從而加深對某個模塊的使用。
1.當前路徑及路徑下的文件
os.getcwd():查看當前所在路徑。
os.listdir(path):列舉目錄下的所有文件。返回的是列表類型。
>>> import os >>> os.getcwd() 'D:\\pythontest\\ostest' >>> os.listdir(os.getcwd()) ['hello.py', 'test.txt']
2.絕對路徑
os.path.abspath(path):返回path的絕對路徑。
>>> os.path.abspath('.') 'D:\\pythontest\\ostest' >>> os.path.abspath('..') 'D:\\pythontest'
3.查看路徑的文件夾部分和文件名部分
os.path.split(path):將路徑分解為(文件夾,文件名),返回的是元組類型。可以看出,若路徑字符串最后一個字符是\,則只有文件夾部分有值;若路徑字符串中均無\,則只有文件名部分有值。若路徑字符串有\,且不在最后,則文件夾和文件名均有值。且返回的文件夾的結果不包含\.
os.path.join(path1,path2,...):將path進行組合,若其中有絕對路徑,則之前的path將被刪除。
>>> os.path.split('D:\\pythontest\\ostest\\Hello.py') ('D:\\pythontest\\ostest', 'Hello.py') >>> os.path.split('.') ('', '.') >>> os.path.split('D:\\pythontest\\ostest\\') ('D:\\pythontest\\ostest', '') >>> os.path.split('D:\\pythontest\\ostest') ('D:\\pythontest', 'ostest') >>> os.path.join('D:\\pythontest', 'ostest') 'D:\\pythontest\\ostest' >>> os.path.join('D:\\pythontest\\ostest', 'hello.py') 'D:\\pythontest\\ostest\\hello.py' >>> os.path.join('D:\\pythontest\\b', 'D:\\pythontest\\a') 'D:\\pythontest\\a'
os.path.dirname(path):返回path中的文件夾部分,結果不包含'\'
>>> os.path.dirname('D:\\pythontest\\ostest\\hello.py') 'D:\\pythontest\\ostest' >>> os.path.dirname('.') '' >>> os.path.dirname('D:\\pythontest\\ostest\\') 'D:\\pythontest\\ostest' >>> os.path.dirname('D:\\pythontest\\ostest') 'D:\\pythontest'
os.path.basename(path):返回path中的文件名。
>>> os.path.basename('D:\\pythontest\\ostest\\hello.py') 'hello.py' >>> os.path.basename('.') '.' >>> os.path.basename('D:\\pythontest\\ostest\\') '' >>> os.path.basename('D:\\pythontest\\ostest') 'ostest'
4.查看文件時間
os.path.getmtime(path):文件或文件夾的最后修改時間,從新紀元到訪問時的秒數。
os.path.getatime(path):文件或文件夾的最后訪問時間,從新紀元到訪問時的秒數。
os.path.getctime(path):文件或文件夾的創建時間,從新紀元到訪問時的秒數。
>>> os.path.getmtime('D:\\pythontest\\ostest\\hello.py') 1481695651.857048 >>> os.path.getatime('D:\\pythontest\\ostest\\hello.py') 1481687717.8506615 >>> os.path.getctime('D:\\pythontest\\ostest\\hello.py') 1481687717.8506615
5.查看文件大小
os.path.getsize(path):文件或文件夾的大小,若是文件夾返回0。
>>> os.path.getsize('D:\\pythontest\\ostest\\hello.py') 58L >>> os.path.getsize('D:\\pythontest\\ostest') 0L
6.查看文件是否存在
os.path.exists(path):文件或文件夾是否存在,返回True 或 False。
>>> os.listdir(os.getcwd()) ['hello.py', 'test.txt'] >>> os.path.exists('D:\\pythontest\\ostest\\hello.py') True >>> os.path.exists('D:\\pythontest\\ostest\\Hello.py') True >>> os.path.exists('D:\\pythontest\\ostest\\Hello1.py') False
7.一些表現形式參數
os中定義了一組文件、路徑在不同操作系統中的表現形式參數,如:
>>> os.sep '\\' >>> os.extsep '.' >>> os.pathsep ';' >>> os.linesep '\r\n'
8.實例說明
在自動化測試過程中,常常需要發送郵件,將最新的測試報告文檔發送給相關人員查看,這是就需要查找最新文件的功能。
舉例:查找文件夾下最新的文件。
代碼如下:
import os def new_file(test_dir): #列舉test_dir目錄下的所有文件(名),結果以列表形式返回。 lists=os.listdir(test_dir) #sort按key的關鍵字進行升序排序,lambda的入參fn為lists列表的元素,獲取文件的最后修改時間,所以最終以文件時間從小到大排序 #最后對lists元素,按文件修改時間大小從小到大排序。 lists.sort(key=lambda fn:os.path.getmtime(test_dir+'\\'+fn)) #獲取最新文件的絕對路徑,列表中最后一個值,文件夾+文件名 file_path=os.path.join(test_dir,lists[-1]) return file_path #返回D:\pythontest\ostest下面最新的文件 print new_file('D:\\system files\\workspace\\selenium\\email126pro\\email126\\report')
運行結果:
最后再啰嗦一句,關於lambda的用法(python中單行的最小函數):
key=lambda fn:os.path.getmtime(test_dir+'\\'+fn) #相當於 def key(fn): return os.path.getmtime(test_dir+'\\'+fn)