Python列出一個(gè)文件夾及其子目錄的所有文件
python簡(jiǎn)介
Python是一種解釋型、面向?qū)ο?、?dòng)態(tài)數(shù)據(jù)類型的高級(jí)程序設(shè)計(jì)語言。
Python由Guido van Rossum于1989年底發(fā)明,第一個(gè)公開發(fā)行版發(fā)行于1991年。
像Perl語言一樣, Python 源代碼同樣遵循 GPL(GNU General Public License)協(xié)議。
>>> import os
>>> for i in os.walk("."):
... print i[0],"\n##",i[1],"\n##",i[2]
...
. #當(dāng)前目錄
## ['fa', 'out'] #當(dāng)前目錄中的子目錄
## ['meta_rna.sh', 'nohup.out', 'log.cpu', 'blast_seq.py']
./fa # 第一個(gè)子目錄
## [] # 第一個(gè)子目錄中的目錄
## ['assemblyar_new_2.faa']
./out # 第二個(gè)子目錄
## [] # 第二個(gè)子目錄中的目錄
## ['assemblyar_new_2.faa.coord', 'assemblyar_new_2.faa.mask', 'assemblyar_new_2.faa.seq', 'result_1.xm', 'result.xml', 'blast_seq.py']
也可以用 os.path.walk, 先定義一個(gè)訪問文件夾的函數(shù), VisitDir
>>> def VisitDir(arg, dirname, names): ... for filespath in names: ... print os.path.join(dirname, filespath) ... >>> path="." >>> os.path.walk(path, VisitDir, ()) ./meta_rna.sh ./fa ./out ./nohup.out ./log.cpu ./blast_seq.py ./fa/assemblyar_new_2.faa ./out/assemblyar_new_2.faa.coord ./out/assemblyar_new_2.faa.mask ./out/assemblyar_new_2.faa.seq ./out/result_1.xm ./out/result.xml ./out/blast_seq.py >>> os.getcwd() '/home/served_pro/Find_nick' >>> abs_path= os.getcwd() >>> os.path.walk(abs_path, VisitDir, ()) /home/served_pro/Find_nick/meta_rna.sh /home/served_pro/Find_nick/fa /home/served_pro/Find_nick/out /home/served_pro/Find_nick/nohup.out /home/served_pro/Find_nick/log.cpu /home/served_pro/Find_nick/blast_seq.py /home/served_pro/Find_nick/fa/assemblyar_new_2.faa /home/served_pro/Find_nick/out/assemblyar_new_2.faa.coord /home/served_pro/Find_nick/out/assemblyar_new_2.faa.mask /home/served_pro/Find_nick/out/assemblyar_new_2.faa.seq /home/served_pro/Find_nick/out/result_1.xm /home/served_pro/Find_nick/out/result.xml /home/served_pro/Find_nick/out/blast_seq.py
下面給大家介紹python列出文件夾下的所有文件
#方法1:使用os.listdir
import os
for filename in os.listdir(r'c:\\windows'):
print filename
#方法2:使用glob模塊,可以設(shè)置文件過濾
import glob
for filename in glob.glob(r'c:\\windows\\*.exe'):
print filename
#方法3:通過os.path.walk遞歸遍歷,可以訪問子文件夾
import os.path
def processDirectory ( args, dirname, filenames ):
print 'Directory',dirname
for filename in filenames:
print ' File',filename
os.path.walk(r'c:\\windows', processDirectory, None )
#方法4:非遞歸
import os
for dirpath, dirnames, filenames in os.walk('c:\\\\winnt'):
print 'Directory', dirpath
for filename in filenames:
print ' File', filename
另外,判斷文件與目錄是否存在:
import os
os.path.isfile('test.txt') #如果不存在就返回False
os.path.exists(directory) #如果目錄不存在就返回False
以上所述是小編給大家介紹的Python列出一個(gè)文件夾及其子目錄的所有文件,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
python中json格式數(shù)據(jù)輸出的簡(jiǎn)單實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猵ython中json格式數(shù)據(jù)輸出的簡(jiǎn)單實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
Python調(diào)用Java可執(zhí)行jar包問題
這篇文章主要介紹了Python調(diào)用Java可執(zhí)行jar包問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
利用Python開發(fā)微信支付的注意事項(xiàng)
如今支付的引入是很多互聯(lián)網(wǎng)產(chǎn)品都需要的。為了讓用戶用著更方便快捷,集成像支付寶、微信支付這樣的第三方支付也就成了常有的事。今天跟著小編就來看看微信支付開發(fā)中幾個(gè)值得注意的地方,涉及代碼之處均用 Python 編寫。2016-08-08
Pandas DataFrame中實(shí)現(xiàn)取單個(gè)值的讀取和修改
這篇文章主要介紹了Pandas DataFrame中實(shí)現(xiàn)取單個(gè)值的讀取和修改,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Pycharm 使用 Pipenv 新建的虛擬環(huán)境(圖文詳解)
pipenv 是 Pipfile 主要倡導(dǎo)者、requests 作者 Kenneth Reitz 寫的一個(gè)命令行工具,主要包含了Pipfile、pip、click、requests和virtualenv。這篇文章主要介紹了Pycharm 使用 Pipenv 新建的虛擬環(huán)境的問題,需要的朋友可以參考下2020-04-04
Python中的 ansible 動(dòng)態(tài)Inventory 腳本
這篇文章主要介紹了Python中的 ansible 動(dòng)態(tài)Inventory 腳本,本章節(jié)通過實(shí)例代碼從mysql數(shù)據(jù)作為數(shù)據(jù)源生成動(dòng)態(tài)ansible主機(jī)為入口介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2020-01-01
Django將項(xiàng)目移動(dòng)到新環(huán)境的操作步驟
本文分步驟給大家介紹Django將項(xiàng)目移動(dòng)到新環(huán)境的方法,通過圖文示例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-08-08

