新聞中心
os模塊提供了多數(shù)操作系統(tǒng)的功能接口函數(shù)。當(dāng)os模塊被導(dǎo)入后,它會自適應(yīng)于不同的操作系統(tǒng)平臺,根據(jù)不同的平臺進(jìn)行相應(yīng)的操作,在python編程時,經(jīng)常和文件、目錄打交道,這時就離不了os模塊,本節(jié)內(nèi)容將對os模塊提供的函數(shù)進(jìn)行詳細(xì)的解讀

1.當(dāng)前路徑及路徑下的文件
os.getcwd():查看當(dāng)前所在路徑。
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):將路徑分解為(文件夾,文件名),返回的是元組類型??梢钥闯?,若路徑字符串最后一個字符是,則只有文件夾部分有值;若路徑字符串中均無,則只有文件名部分有值。若路徑字符串有\(zhòng),且不在最后,則文件夾和文件名均有值。且返回的文件夾的結(jié)果不包含.
os.path.join(path1,path2,…):將path進(jìn)行組合,若其中有絕對路徑,則之前的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中的文件夾部分,結(jié)果不包含”
>>> 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):文件或文件夾的最后修改時間,從新紀(jì)元到訪問時的秒數(shù)。
os.path.getatime(path):文件或文件夾的最后訪問時間,從新紀(jì)元到訪問時的秒數(shù)。
os.path.getctime(path):文件或文件夾的創(chuàng)建時間,從新紀(jì)元到訪問時的秒數(shù)。
>>> 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.一些表現(xiàn)形式參數(shù)
os中定義了一組文件、路徑在不同操作系統(tǒng)中的表現(xiàn)形式參數(shù),如:
>>> os.sep
'\\'
>>> os.extsep
'.'
>>> os.pathsep
';'
>>> os.linesep
'\r\n'
8.實例說明
在自動化測試過程中,常常需要發(fā)送郵件,將最新的測試報告文檔發(fā)送給相關(guān)人員查看,這是就需要查找最新文件的功能。
舉例:查找文件夾下最新的文件。
img
代碼如下:
import os
def new_file(test_dir):
#列舉test_dir目錄下的所有文件(名),結(jié)果以列表形式返回。
lists=os.listdir(test_dir)
#sort按key的關(guān)鍵字進(jìn)行升序排序,lambda的入?yún)n為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')
運(yùn)行結(jié)果:
img
最后再啰嗦一句,關(guān)于lambda的用法(python中單行的最小函數(shù)):
key=lambda fn:os.path.getmtime(test_dir+'\\'+fn)
#相當(dāng)于
def key(fn):
return os.path.getmtime(test_dir+'\\'+fn)
分享題目:詳解python模塊os
分享URL:http://fisionsoft.com.cn/article/dphggjs.html


咨詢
建站咨詢
