日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

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

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

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):將路徑分解為(文件夾,文件名),返回的是元組類型。可以看出,若路徑字符串最后一個字符是,則只有文件夾部分有值;若路徑字符串中均無,則只有文件名部分有值。若路徑字符串有\(zhòng),且不在最后,則文件夾和文件名均有值。且返回的文件夾的結果不包含.

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):文件或文件夾的最后修改時間,從新紀元到訪問時的秒數(shù)。

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

os.path.getctime(path):文件或文件夾的創(chuàng)建時間,從新紀元到訪問時的秒數(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ā)送給相關人員查看,這是就需要查找最新文件的功能。

舉例:查找文件夾下最新的文件。

img

代碼如下:

import os
def new_file(test_dir):
   #列舉test_dir目錄下的所有文件(名),結果以列表形式返回。
   lists=os.listdir(test_dir)
   #sort按key的關鍵字進行升序排序,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')

運行結果:

img

最后再啰嗦一句,關于lambda的用法(python中單行的最小函數(shù)):

key=lambda fn:os.path.getmtime(test_dir+'\\'+fn)
#相當于
def key(fn):
   return os.path.getmtime(test_dir+'\\'+fn)

分享文章:詳解python模塊os
鏈接分享:http://www.dlmjj.cn/article/dphggjs.html