The example in this article describes how Python gets all the list of file names in the specified directory. Share to everyone for your reference. The implementation method is as follows:
This Python code implements the ability to get a list of file names that can be used to specify the characters contained in the file to facilitate the extraction of a particular type of file name list:
#-*-Coding:utf-8-*-#~ #------------------------------------------------------------------#~ Module:wlab #~ Filename:wgetfilelist.py #~ function: #~ def issubstring (substrlist,str) #~ def getfilelist (findpath,flagstr=[]): #~ function: Read list of specific types of file names in the specified directory #~ data:2013-08-08, Thursday #~ Author: Wu Xuping #~ 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 Su BSTR 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
You can install Wlab online using PIP
Let's give it a picture:
Hopefully this article will help you with Python programming.