日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
詳解python模塊os

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

1.當(dāng)前路徑及路徑下的文件

os.getcwd():查看當(dāng)前所在路徑。

os.listdir(path):列舉目錄下的所有文件。返回的是列表類(lèi)型。

>>> import os
>>> os.getcwd()
'D:\\pythontest\\ostest'
>>> os.listdir(os.getcwd())
['hello.py', 'test.txt']

2.絕對(duì)路徑

os.path.abspath(path):返回path的絕對(duì)路徑。

>>> os.path.abspath('.')
'D:\\pythontest\\ostest'
>>> os.path.abspath('..')
'D:\\pythontest'

3.查看路徑的文件夾部分和文件名部分

os.path.split(path):將路徑分解為(文件夾,文件名),返回的是元組類(lèi)型??梢钥闯?,若路徑字符串最后一個(gè)字符是,則只有文件夾部分有值;若路徑字符串中均無(wú),則只有文件名部分有值。若路徑字符串有\(zhòng),且不在最后,則文件夾和文件名均有值。且返回的文件夾的結(jié)果不包含.

os.path.join(path1,path2,…):將path進(jìn)行組合,若其中有絕對(duì)路徑,則之前的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.查看文件時(shí)間

os.path.getmtime(path):文件或文件夾的最后修改時(shí)間,從新紀(jì)元到訪問(wèn)時(shí)的秒數(shù)。

os.path.getatime(path):文件或文件夾的最后訪問(wèn)時(shí)間,從新紀(jì)元到訪問(wèn)時(shí)的秒數(shù)。

os.path.getctime(path):文件或文件夾的創(chuàng)建時(shí)間,從新紀(jì)元到訪問(wèn)時(shí)的秒數(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.實(shí)例說(shuō)明

在自動(dòng)化測(cè)試過(guò)程中,常常需要發(fā)送郵件,將最新的測(cè)試報(bào)告文檔發(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列表的元素,獲取文件的最后修改時(shí)間,所以最終以文件時(shí)間從小到大排序
   #最后對(duì)lists元素,按文件修改時(shí)間大小從小到大排序。
   lists.sort(key=lambda fn:os.path.getmtime(test_dir+'\\'+fn))
   #獲取最新文件的絕對(duì)路徑,列表中最后一個(gè)值,文件夾+文件名
   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)

文章名稱(chēng):詳解python模塊os
瀏覽地址:http://www.5511xx.com/article/dphggjs.html