Python列出一個(gè)文件夾及其子目錄的所有文件
python簡介
Python是一種解釋型、面向?qū)ο蟆討B(tài)數(shù)據(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è)文件夾及其子目錄的所有文件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
python正則表達(dá)式去掉數(shù)字中的逗號(python正則匹配逗號)
在處理自然語言時(shí)123,000,000如果以標(biāo)點(diǎn)符號分割,就會出現(xiàn)問題,好好的一個(gè)數(shù)字就被逗號肢解了,因此可以先下手把數(shù)字處理干凈(逗號去掉)2013-12-12基于Python中單例模式的幾種實(shí)現(xiàn)方式及優(yōu)化詳解
下面小編就為大家分享一篇基于Python中單例模式的幾種實(shí)現(xiàn)方式及優(yōu)化詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Python實(shí)現(xiàn)注冊登錄系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了適合初學(xué)者學(xué)習(xí)的Python3銀行賬戶登錄系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08命令行運(yùn)行Python腳本時(shí)傳入?yún)?shù)的三種方式詳解
這篇文章主要介紹了命令行運(yùn)行Python腳本時(shí)傳入?yún)?shù)的三種方式詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10淺談tensorflow中Dataset圖片的批量讀取及維度的操作詳解
今天小編就為大家分享一篇淺談tensorflow中Dataset圖片的批量讀取及維度的操作詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01