The example in this article tells Python how to get all the list of file names in the specified directory. Share to everyone for your reference. The implementation methods are as follows:
Here the Python code implements the function of getting the list of file names, can specify the characters contained in the file to facilitate the extraction of a specific type of file name list:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the |
#-*-Coding:utf-8-*-#~ #------------------------------------------------------------------#~ module:wlab #~-Filena me:wgetfilelist.py #~ function: #~ def issubstring (substrlist,str) #~ def getfilelist (findpath,flagstr=[)): #~ function: Read the specified directory List of specific types of file names #~ 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 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 |
You can use Pip to install Wlab online
?
Let's give you a picture:
I hope this article will help you with your Python programming.