這篇文章主要介紹python如何列出文件夾所有文件,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、自適應(yīng)網(wǎng)站建設(shè)、程序開發(fā)、微網(wǎng)站、重慶小程序開發(fā)等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營(yíng)銷、管理等多方位專業(yè)化運(yùn)作于一體,具備承接不同規(guī)模與類型的建設(shè)項(xiàng)目的能力。導(dǎo)入庫(kù)
首先,我們需要os庫(kù)中的三種方法。
l listdir:檢索目錄的內(nèi)容(目錄和文件)。
l join:將兩個(gè)組件組合成一條路徑。
l isfile:如果給定的路徑組件是一個(gè)文件,則返回true。
from os import listdir from os.path import join, isfile
檢索目錄的內(nèi)容
導(dǎo)入必要的方法后,該設(shè)置目錄的路徑并檢索其內(nèi)容了。
from os import listdir from os.path import join, isfile directory_path = "/some/path" contents = listdir(directory_path)
篩選目錄
現(xiàn)在我們有了目錄的所有內(nèi)容,是時(shí)候過(guò)濾掉目錄了—僅保留文件。
from os import listdir from os.path import join, isfile directory_path = "/some/path" contents = listdir(directory_path) files = filter(lambda f: isfile(join(directory_path,f)),contents)
files為列表以打印內(nèi)容
from os import listdir from os.path import join, isfile directory_path = "/Users/jhsu/Desktop" contents = listdir(directory_path) files = filter(lambda f: isfile(join(directory_path,f)),contents) print(files) # <filter object at 0x10a5203a0> print(list(files)) # [list of files]
以上是python如何列出文件夾所有文件的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞名稱:python如何列出文件夾所有文件-創(chuàng)新互聯(lián)
分享路徑:http://aaarwkj.com/article44/goeee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站建設(shè)、微信小程序、靜態(tài)網(wǎng)站、外貿(mào)建站、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容