This article example describes the python method to get a list of all filenames in a specified directory. Shared for your reference. Specific implementation methods are as follows:
Here python code to implement the function of obtaining the list of filenames, you can specify the characters contained in the file to facilitate the extraction of a specific type of filename list:
# -*- coding: utf-8 -*- #~ #------------------------------------------------------------------ #~ module:wlab #~ Filename: #~ Function : #~ def IsSubString(SubStrList,Str) #~ def GetFileList(FindPath,FlagStr=[]): #~ Function:Reads a list of file names of a specific type in a specified directory. #~ Data: 2013-08-08,Thursday #~ Author:Wu Xu Ping #~ Email:wxp07@ #~ #------------------------------------------------------------------ #~ #------------------------------------------------------------------ def IsSubString(SubStrList,Str): ''''' # Determine whether the string Str contains every substring in the sequence SubStrList. #>>>SubStrList=['F','EMS','txt'] #>>>Str='' #>>>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=[]): ''''' # Get the name of the specified file in the directory #>>>FlagStr=['F','EMS','txt'] #Require these characters in the file name #>>>FileList=GetFileList(FindPath,FlagStr) # ''' import os FileList=[] FileNames=(FindPath) if (len(FileNames)>0): for fn in FileNames: if (len(FlagStr)>0): # Returns a filename of the specified type if (IsSubString(FlagStr,fn)): fullfilename=(FindPath,fn) (fullfilename) else: #The default is to return all filenames directly fullfilename=(FindPath,fn) (fullfilename) # Sort file names if (len(FileList)>0): () return FileList
You can install wlab online using pip
pip install wlab
Better give a picture:
I hope that what I have described in this article will help you in your Python programming.