日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python os模塊簡(jiǎn)單應(yīng)用示例

系統(tǒng) 1743 0

本文實(shí)例講述了python os模塊簡(jiǎn)單應(yīng)用。分享給大家供大家參考,具體如下:

舉例中的目錄形式如下所示:

            
In [36]: pwd
Out[36]: '/home/python/Desktop/code'
In [37]: ls
hello.py hello.txt test.py 文件夾01/ 文件夾02/ 文件夾03/


          

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

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

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

            
In [1]: import os
In [2]: os.getcwd()
Out[2]: '/home/python/Desktop/code'
In [3]: os.listdir(os.getcwd())
Out[3]: ['文件夾01', '文件夾03', '文件夾02', 'test.py', '.idea', 'hello.txt', 'hello.py']


          

2.絕對(duì)路徑

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

            
In [4]: os.path.abspath('.')
Out[4]: '/home/python/Desktop/code'
In [5]: os.path.abspath('..')
Out[5]: '/home/python/Desktop'


          

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

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

            
In [6]: os.path.split('.')
Out[6]: ('', '.')
In [7]: os.path.split('/home')
Out[7]: ('/', 'home')
In [8]: os.path.split('/home/Desktop')
Out[8]: ('/home', 'Desktop')
In [9]: os.path.split('/home/Desktop/code')
Out[9]: ('/home/Desktop', 'code')
In [10]: os.path.split('/home/Desktop/code/')
Out[10]: ('/home/Desktop/code', '')


          

os.path.join(path1,path2,…) :將path進(jìn)行組合,若其中有絕對(duì)路徑,則之前的path將被刪除。

            
In [12]: os.path.join('/home', 'Desktop')
Out[12]: '/home/Desktop'
In [13]: os.path.join('/home/Desktop', 'code')
Out[13]: '/home/Desktop/code'


          

os.path.dirname(path) :返回path中的文件夾部分,結(jié)果不包含'\'

            
In [14]: os.path.dirname(os.getcwd())
Out[14]: '/home/python/Desktop'


          

os.path.basename(path) :返回path中的文件名。

            
In [15]: os.path.basename(os.getcwd())
Out[15]: 'code'
In [16]: os.path.basename('.')
Out[16]: '.'
In [17]: os.path.basename('/home/Desktop/code')
Out[17]: 'code'
In [18]: os.path.basename('/home/Desktop/code/')
Out[18]: ''
In [19]: os.path.basename('/home/Desktop/code/hello.txt')
Out[19]: 'hello.txt'


          

4.查看文件時(shí)間

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

            
In [20]: os.path.getmtime(os.getcwd())
Out[20]: 1503292529.869008


          

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

            
In [21]: os.path.getatime(os.getcwd())
Out[21]: 1503292529.8930087


          

os.path.getctime(path) :文件或文件夾的創(chuàng)建時(shí)間,從新紀(jì)元到訪問(wèn)時(shí)的秒數(shù)。

            
In [22]: os.path.getctime(os.getcwd())
Out[22]: 1503292529.869008


          

5.查看文件大小

os.path.getsize(path) :文件或文件夾的大小。

            
In [25]: os.getcwd()
Out[25]: '/home/python/Desktop/code'
In [26]: os.path.getsize('/home/python/Desktop/code')
Out[26]: 4096
In [28]: os.path.getsize('/home/python/Desktop/code/hello.txt')
Out[28]: 61


          

6.查看文件是否存在

os.path.exists(path) :文件或文件夾是否存在,返回True 或 False。

            
In [29]: os.path.exists('/home/python/Desktop/code/hello.txt')
Out[29]: True
In [30]: os.path.exists('/home/python/Desktop/code/hehe.txt')
Out[30]: False


          

7.一些表現(xiàn)形式參數(shù)

os中定義了一組文件、路徑在不同操作系統(tǒng)中的表現(xiàn)形式參數(shù),如:

            
In [31]: os.sep
Out[31]: '/'
In [32]: os.extsep
Out[32]: '.'
In [33]: os.linesep
Out[33]: '\n'
In [34]: os.pathsep
Out[34]: ':'


          

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 玉田县| 卫辉市| 濉溪县| 广东省| 牡丹江市| 宣威市| 梁河县| 巴彦县| 呼伦贝尔市| 德惠市| 富裕县| 贡觉县| 北海市| 沾益县| 诏安县| 长丰县| 阳高县| 乌恰县| 华容县| 临泉县| 策勒县| 富源县| 富裕县| 绥化市| 赤峰市| 沈丘县| 五常市| 乌拉特后旗| 西宁市| 太谷县| 白城市| 禹州市| 安宁市| 昆明市| 英德市| 虞城县| 田阳县| 新民市| 四子王旗| 年辖:市辖区| 桦川县|