python擷取指定目錄下所有檔案名稱列表的方法

來源:互聯網
上載者:User

   本文執行個體講述了python擷取指定目錄下所有檔案名稱列表的方法。分享給大家供大家參考。具體實現方法如下:

  這裡python代碼實現擷取檔案名稱列表的功能,可以指定檔案中包含的字元,方便提取特定類型的檔案名稱列表:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 # -*- coding: utf-8 -*- #~ #------------------------------------------------------------------ #~ module:wlab #~ Filename:wgetfilelist.py #~ Function : #~ def IsSubString(SubStrList,Str) #~ def GetFileList(FindPath,FlagStr=[]): #~ 功能:讀取指定目錄下特定類型的檔案名稱列表 #~ Data: 2013-08-08,星期四 #~ Author:吳徐平 #~ Email:wxp07@qq.com #~ #------------------------------------------------------------------ #~ #------------------------------------------------------------------ def IsSubString(SubStrList,Str): ''''' #判斷字串Str是否包含序列SubStrList中的每一個子字串 #>>>SubStrList=['F','EMS','txt'] #>>>Str='F06925EMS91.txt' #>>>IsSubString(SubStrList,Str)#return True (or False) ''' flag=True for substr in SubStrList: if not(substr in Str): flag=False return flag #~ #---------------------------------------------------------------------- def GetFileList(FindPath,FlagStr=[]): ''''' #擷取目錄中指定的檔案名稱 #>>>FlagStr=['F','EMS','txt'] #要求檔案名稱中包含這些字元 #>>>FileList=GetFileList(FindPath,FlagStr) # ''' import os FileList=[] FileNames=os.listdir(FindPath) if (len(FileNames)>0): for fn in FileNames: if (len(FlagStr)>0): #返回指定類型的檔案名稱 if (IsSubString(FlagStr,fn)): fullfilename=os.path.join(FindPath,fn) FileList.append(fullfilename) else: #預設直接返回所有檔案名稱 fullfilename=os.path.join(FindPath,fn) FileList.append(fullfilename) #對檔案名稱排序 if (len(FileList)>0): FileList.sort() return FileList

  可以使用pip線上安裝wlab

  ?

1 pip install wlab

  還是給個圖吧:

  希望本文所述對大家的Python程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.