Python列出一個檔案夾及其子目錄的所有檔案

來源:互聯網
上載者:User
python簡介

Python是一種解釋型、物件導向、動態資料類型的進階程式設計語言。

Python由Guido van Rossum於1989年底發明,第一個公開發行版發行於1991年。

像Perl語言一樣, Python 原始碼同樣遵循 GPL(GNU General Public License)協議。

>>> import os>>> for i in os.walk("."):... print i[0],"\n##",i[1],"\n##",i[2]... . #目前的目錄## ['fa', 'out'] #目前的目錄中的子目錄 ## ['meta_rna.sh', 'nohup.out', 'log.cpu', 'blast_seq.py']./fa # 第一個子目錄## [] # 第一個子目錄中的目錄## ['assemblyar_new_2.faa']./out # 第二個子目錄## [] # 第二個子目錄中的目錄## ['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, 先定義一個訪問檔案夾的函數, 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.listdirimport osfor filename in os.listdir(r'c:\\windows'):print filename#方法2:使用glob模組,可以設定檔案過濾import globfor filename in glob.glob(r'c:\\windows\\*.exe'):print filename#方法3:通過os.path.walk遞迴遍曆,可以訪問子檔案夾import os.pathdef processDirectory ( args, dirname, filenames ):print 'Directory',dirnamefor filename in filenames:print ' File',filenameos.path.walk(r'c:\\windows', processDirectory, None )#方法4:非遞迴import osfor dirpath, dirnames, filenames in os.walk('c:\\\\winnt'):print 'Directory', dirpathfor filename in filenames:print ' File', filename

另外,判斷檔案與目錄是否存在:

import osos.path.isfile('test.txt') #如果不存在就返回Falseos.path.exists(directory) #如果目錄不存在就返回False

以上所述是小編給大家介紹的Python列出一個檔案夾及其子目錄的所有檔案,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對topic.alibabacloud.com的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.